@@ -523,6 +523,85 @@ describe('raven.Client', function () {
523523 } ) ;
524524 } ) ;
525525
526+ describe ( 'sampleRate' , function ( ) {
527+ var origRandom ;
528+ beforeEach ( function ( ) {
529+ origRandom = Math . random ;
530+ Math . random = function ( ) {
531+ return 0.5 ;
532+ } ;
533+ } ) ;
534+
535+ afterEach ( function ( ) {
536+ Math . random = origRandom ;
537+ } ) ;
538+
539+ it ( 'should respect sampleRate to omit event' , function ( done ) {
540+ client = new raven . Client ( dsn , {
541+ sampleRate : 0.3
542+ } ) ;
543+
544+ client . process ( {
545+ message : 'test'
546+ } , function ( err , eventId ) {
547+ setTimeout ( done , 10 ) ;
548+ } ) ;
549+ } ) ;
550+
551+ it ( 'should respect sampleRate to include event' , function ( done ) {
552+ var scope = nock ( 'https://app.getsentry.com' )
553+ . filteringRequestBody ( / .* / , '*' )
554+ . post ( '/api/269/store/' , '*' )
555+ . reply ( 200 , function ( uri , body ) {
556+ zlib . inflate ( new Buffer ( body , 'base64' ) , function ( err , dec ) {
557+ if ( err ) return done ( err ) ;
558+ var msg = JSON . parse ( dec . toString ( ) ) ;
559+ var extra = msg . extra ;
560+
561+ extra . should . have . property ( 'foo' ) ;
562+ done ( ) ;
563+ } ) ;
564+ return 'OK' ;
565+ } ) ;
566+
567+ client = new raven . Client ( dsn , {
568+ sampleRate : 0.8
569+ } ) ;
570+
571+ client . process ( {
572+ message : 'test' ,
573+ extra : {
574+ foo : 'bar'
575+ }
576+ } ) ;
577+
578+ client . on ( 'logged' , function ( ) {
579+ scope . done ( ) ;
580+ } ) ;
581+ } ) ;
582+
583+ it ( 'should always send if sampleRate is omitted' , function ( done ) {
584+ var scope = nock ( 'https://app.getsentry.com' )
585+ . filteringRequestBody ( / .* / , '*' )
586+ . post ( '/api/269/store/' , '*' )
587+ . reply ( 200 , function ( uri , body ) {
588+ zlib . inflate ( new Buffer ( body , 'base64' ) , function ( err , dec ) {
589+ if ( err ) return done ( err ) ;
590+ done ( ) ;
591+ } ) ;
592+ return 'OK' ;
593+ } ) ;
594+
595+ client . process ( {
596+ message : 'test'
597+ } ) ;
598+
599+ client . on ( 'logged' , function ( ) {
600+ scope . done ( ) ;
601+ } ) ;
602+ } ) ;
603+ } ) ;
604+
526605 it ( 'should call the callback after sending' , function ( done ) {
527606 var firedCallback = false ;
528607 var sentResponse = false ;
0 commit comments