@@ -618,31 +618,99 @@ describe('WidgetModel', function () {
618618 } ) ;
619619
620620 it ( 'respects the message throttle' , function ( ) {
621- const send = sinon . spy ( this . widget , 'send_sync_message' ) ;
621+ // reuse the callback, similar to .touch in views, which will
622+ // expose an bug of calling onstatus multiple times
623+ const callbacks = this . widget . callbacks ( ) ;
622624 this . widget . set ( 'a' , 'sync test' ) ;
623- this . widget . save_changes ( ) ;
625+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
626+ this . widget . save_changes ( callbacks ) ;
627+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
624628 this . widget . set ( 'a' , 'another sync test' ) ;
625629 this . widget . set ( 'b' , 'change b' ) ;
626- this . widget . save_changes ( ) ;
630+ this . widget . save_changes ( callbacks ) ;
627631 this . widget . set ( 'b' , 'change b again' ) ;
628- this . widget . save_changes ( ) ;
632+ this . widget . save_changes ( callbacks ) ;
633+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
629634
630635 // check that one sync message went through
631- expect ( send ) . to . be . calledOnce ;
632- expect ( send ) . to . be . calledWith ( {
633- a : 'sync test' ,
636+ expect ( this . comm . send ) . to . be . calledOnce ;
637+ expect ( this . comm . send ) . to . be . calledWith ( {
638+ method : 'update' ,
639+ state : { a : 'sync test' } ,
640+ buffer_paths : [ ] ,
634641 } ) ;
642+ expect ( this . widget . _msg_buffer ) . to . deep . equal ( {
643+ a : 'another sync test' ,
644+ b : 'change b again' ,
645+ } ) ;
646+ expect ( this . widget . _msg_buffer_callbacks ) . to . not . equals ( null ) ;
635647 // have the comm send a status idle message
636- this . widget . _handle_status ( {
648+ callbacks . iopub . status ( {
637649 content : {
638650 execution_state : 'idle' ,
639651 } ,
640652 } ) ;
653+ // we directly get a new pending message
654+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
655+ expect ( this . widget . _msg_buffer ) . to . equal ( null ) ;
656+ expect ( this . widget . _msg_buffer_callbacks ) . to . equals ( null ) ;
657+
641658 // check that the other sync message went through with the updated values
642- expect ( send . secondCall ) . to . be . calledWith ( {
643- a : 'another sync test' ,
644- b : 'change b again' ,
659+ expect ( this . comm . send . secondCall ) . to . be . calledWith ( {
660+ method : 'update' ,
661+ state : {
662+ a : 'another sync test' ,
663+ b : 'change b again' ,
664+ } ,
665+ buffer_paths : [ ] ,
666+ } ) ;
667+ callbacks . iopub . status ( {
668+ content : {
669+ execution_state : 'idle' ,
670+ } ,
671+ } ) ;
672+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
673+
674+ // repeat again
675+ this . comm . send . resetHistory ( ) ;
676+
677+ this . widget . set ( 'a' , 'sync test - 2' ) ;
678+ this . widget . save_changes ( callbacks ) ;
679+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
680+ this . widget . set ( 'a' , 'another sync test - 2' ) ;
681+ this . widget . set ( 'b' , 'change b - 2' ) ;
682+ this . widget . save_changes ( callbacks ) ;
683+ this . widget . set ( 'b' , 'change b again - 2' ) ;
684+ this . widget . save_changes ( callbacks ) ;
685+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
686+ expect ( this . comm . send ) . to . be . calledOnce ;
687+ expect ( this . comm . send ) . to . be . calledWith ( {
688+ method : 'update' ,
689+ state : { a : 'sync test - 2' } ,
690+ buffer_paths : [ ] ,
691+ } ) ;
692+ callbacks . iopub . status ( {
693+ content : {
694+ execution_state : 'idle' ,
695+ } ,
696+ } ) ;
697+ // again, directly a new message
698+ expect ( this . widget . _pending_msgs ) . to . equal ( 1 ) ;
699+
700+ expect ( this . comm . send . secondCall ) . to . be . calledWith ( {
701+ method : 'update' ,
702+ state : {
703+ a : 'another sync test - 2' ,
704+ b : 'change b again - 2' ,
705+ } ,
706+ buffer_paths : [ ] ,
707+ } ) ;
708+ callbacks . iopub . status ( {
709+ content : {
710+ execution_state : 'idle' ,
711+ } ,
645712 } ) ;
713+ expect ( this . widget . _pending_msgs ) . to . equal ( 0 ) ;
646714 } ) ;
647715
648716 it ( 'Initial values are *not* sent on creation' , function ( ) {
0 commit comments