@@ -616,31 +616,99 @@ describe('WidgetModel', function () {
616616 } ) ;
617617
618618 it ( 'respects the message throttle' , function ( ) {
619- const send = sinon . spy ( this . widget , 'send_sync_message' ) ;
619+ // reuse the callback, similar to .touch in views, which will
620+ // expose an bug of calling onstatus multiple times
621+ const callbacks = this . widget . callbacks ( ) ;
620622 this . widget . set ( 'a' , 'sync test' ) ;
621- this . widget . save_changes ( ) ;
623+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
624+ this . widget . save_changes ( callbacks ) ;
625+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
622626 this . widget . set ( 'a' , 'another sync test' ) ;
623627 this . widget . set ( 'b' , 'change b' ) ;
624- this . widget . save_changes ( ) ;
628+ this . widget . save_changes ( callbacks ) ;
625629 this . widget . set ( 'b' , 'change b again' ) ;
626- this . widget . save_changes ( ) ;
630+ this . widget . save_changes ( callbacks ) ;
631+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
627632
628633 // check that one sync message went through
629- expect ( send ) . to . be . calledOnce ;
630- expect ( send ) . to . be . calledWith ( {
631- a : 'sync test' ,
634+ expect ( this . comm . send ) . to . be . calledOnce ;
635+ expect ( this . comm . send ) . to . be . calledWith ( {
636+ method : 'update' ,
637+ state : { a : 'sync test' } ,
638+ buffer_paths : [ ] ,
639+ } ) ;
640+ expect ( this . widget . _msg_buffer ) . to . deep . equal ( {
641+ a : 'another sync test' ,
642+ b : 'change b again' ,
632643 } ) ;
644+ expect ( this . widget . _msg_buffer_callbacks ) . to . not . equals ( null ) ;
633645 // have the comm send a status idle message
634- this . widget . _handle_status ( {
646+ callbacks . iopub . status ( {
635647 content : {
636648 execution_state : 'idle' ,
637649 } ,
638650 } ) ;
651+ // we directly get a new pending message
652+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
653+ expect ( this . widget . _msg_buffer ) . to . equal ( null ) ;
654+ expect ( this . widget . _msg_buffer_callbacks ) . to . equals ( null ) ;
655+
639656 // check that the other sync message went through with the updated values
640- expect ( send . secondCall ) . to . be . calledWith ( {
641- a : 'another sync test' ,
642- b : 'change b again' ,
657+ expect ( this . comm . send . secondCall ) . to . be . calledWith ( {
658+ method : 'update' ,
659+ state : {
660+ a : 'another sync test' ,
661+ b : 'change b again' ,
662+ } ,
663+ buffer_paths : [ ] ,
664+ } ) ;
665+ callbacks . iopub . status ( {
666+ content : {
667+ execution_state : 'idle' ,
668+ } ,
669+ } ) ;
670+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
671+
672+ // repeat again
673+ this . comm . send . resetHistory ( ) ;
674+
675+ this . widget . set ( 'a' , 'sync test - 2' ) ;
676+ this . widget . save_changes ( callbacks ) ;
677+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
678+ this . widget . set ( 'a' , 'another sync test - 2' ) ;
679+ this . widget . set ( 'b' , 'change b - 2' ) ;
680+ this . widget . save_changes ( callbacks ) ;
681+ this . widget . set ( 'b' , 'change b again - 2' ) ;
682+ this . widget . save_changes ( callbacks ) ;
683+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
684+ expect ( this . comm . send ) . to . be . calledOnce ;
685+ expect ( this . comm . send ) . to . be . calledWith ( {
686+ method : 'update' ,
687+ state : { a : 'sync test - 2' } ,
688+ buffer_paths : [ ] ,
689+ } ) ;
690+ callbacks . iopub . status ( {
691+ content : {
692+ execution_state : 'idle' ,
693+ } ,
694+ } ) ;
695+ // again, directly a new message
696+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
697+
698+ expect ( this . comm . send . secondCall ) . to . be . calledWith ( {
699+ method : 'update' ,
700+ state : {
701+ a : 'another sync test - 2' ,
702+ b : 'change b again - 2' ,
703+ } ,
704+ buffer_paths : [ ] ,
705+ } ) ;
706+ callbacks . iopub . status ( {
707+ content : {
708+ execution_state : 'idle' ,
709+ } ,
643710 } ) ;
711+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
644712 } ) ;
645713
646714 it ( 'Initial values are *not* sent on creation' , function ( ) {
0 commit comments