@@ -92,7 +92,6 @@ describe('SyncEngineLevel', () => {
92
92
watermark,
93
93
messageCid
94
94
} ) ;
95
- console . log ( 'key' , key ) ;
96
95
97
96
const syncParams = SyncEngineLevel [ 'parseSyncMessageParamsKey' ] ( key ) ;
98
97
expect ( syncParams . protocol ) . to . be . undefined ;
@@ -152,7 +151,6 @@ describe('SyncEngineLevel', () => {
152
151
153
152
sinon . restore ( ) ;
154
153
155
- syncEngine . stopSync ( ) ;
156
154
await syncEngine . clear ( ) ;
157
155
await testHarness . syncStore . clear ( ) ;
158
156
await testHarness . dwnDataStore . clear ( ) ;
@@ -471,7 +469,7 @@ describe('SyncEngineLevel', () => {
471
469
did : alice . did . uri ,
472
470
} ) ;
473
471
474
- const clock = sinon . useFakeTimers ( ) ;
472
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
475
473
sinon . stub ( syncEngine as any , 'push' ) . resolves ( ) ;
476
474
const pullSpy = sinon . stub ( syncEngine as any , 'pull' ) ;
477
475
pullSpy . returns ( new Promise < void > ( ( resolve ) => {
@@ -2444,7 +2442,7 @@ describe('SyncEngineLevel', () => {
2444
2442
const pushSpy = sinon . stub ( SyncEngineLevel . prototype as any , 'push' ) ;
2445
2443
pushSpy . resolves ( ) ;
2446
2444
2447
- const clock = sinon . useFakeTimers ( ) ;
2445
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2448
2446
2449
2447
testHarness . agent . sync . startSync ( { interval : '500ms' } ) ;
2450
2448
@@ -2463,7 +2461,7 @@ describe('SyncEngineLevel', () => {
2463
2461
did : alice . did . uri ,
2464
2462
} ) ;
2465
2463
2466
- const clock = sinon . useFakeTimers ( ) ;
2464
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2467
2465
2468
2466
const pullSpy = sinon . stub ( SyncEngineLevel . prototype as any , 'pull' ) ;
2469
2467
pullSpy . returns ( new Promise < void > ( ( resolve ) => {
@@ -2505,7 +2503,7 @@ describe('SyncEngineLevel', () => {
2505
2503
did : alice . did . uri ,
2506
2504
} ) ;
2507
2505
2508
- const clock = sinon . useFakeTimers ( ) ;
2506
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2509
2507
2510
2508
const syncSpy = sinon . stub ( SyncEngineLevel . prototype as any , 'sync' ) ;
2511
2509
// set to be a sync time longer than the interval
@@ -2551,7 +2549,7 @@ describe('SyncEngineLevel', () => {
2551
2549
did : alice . did . uri ,
2552
2550
} ) ;
2553
2551
2554
- const clock = sinon . useFakeTimers ( ) ;
2552
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2555
2553
2556
2554
const syncSpy = sinon . stub ( SyncEngineLevel . prototype as any , 'sync' ) ;
2557
2555
// set to be a sync time longer than the interval
@@ -2586,5 +2584,270 @@ describe('SyncEngineLevel', () => {
2586
2584
clock . restore ( ) ;
2587
2585
} ) ;
2588
2586
} ) ;
2587
+
2588
+ describe ( 'stopSync()' , ( ) => {
2589
+ it ( 'stops the sync interval' , async ( ) => {
2590
+ await testHarness . agent . sync . registerIdentity ( {
2591
+ did : alice . did . uri ,
2592
+ } ) ;
2593
+
2594
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2595
+
2596
+ const syncSpy = sinon . spy ( SyncEngineLevel . prototype as any , 'sync' ) ;
2597
+
2598
+ // stub push and pull to take 3 ms each
2599
+ const pullStub = sinon . stub ( SyncEngineLevel . prototype as any , 'pull' ) ;
2600
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2601
+ clock . setTimeout ( ( ) => {
2602
+ resolve ( ) ;
2603
+ } , 3 ) ;
2604
+ } ) ) ;
2605
+
2606
+ const pushStub = sinon . stub ( SyncEngineLevel . prototype as any , 'push' ) ;
2607
+ pushStub . returns ( new Promise < void > ( ( resolve ) => {
2608
+ clock . setTimeout ( ( ) => {
2609
+ resolve ( ) ;
2610
+ } , 3 ) ;
2611
+ } ) ) ;
2612
+
2613
+ testHarness . agent . sync . startSync ( { interval : '500ms' } ) ;
2614
+
2615
+ // expect the immediate sync call
2616
+ expect ( syncSpy . callCount ) . to . equal ( 1 ) ;
2617
+
2618
+
2619
+ await clock . tickAsync ( 1_300 ) ; // just under 3 intervals
2620
+
2621
+ // expect 2 sync interval calls + initial sync
2622
+ expect ( syncSpy . callCount ) . to . equal ( 3 ) ;
2623
+
2624
+ await testHarness . agent . sync . stopSync ( ) ;
2625
+
2626
+ await clock . tickAsync ( 1_000 ) ; // 2 intervals
2627
+
2628
+ // sync calls remain unchanged
2629
+ expect ( syncSpy . callCount ) . to . equal ( 3 ) ;
2630
+
2631
+ syncSpy . restore ( ) ;
2632
+ clock . restore ( ) ;
2633
+ } ) ;
2634
+
2635
+ it ( 'waits for the current sync to complete before stopping' , async ( ) => {
2636
+ await testHarness . agent . sync . registerIdentity ( {
2637
+ did : alice . did . uri ,
2638
+ } ) ;
2639
+
2640
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2641
+
2642
+ const syncSpy = sinon . spy ( SyncEngineLevel . prototype as any , 'sync' ) ;
2643
+
2644
+ // stub push and pull to take 3 ms each
2645
+ const pullStub = sinon . stub ( SyncEngineLevel . prototype as any , 'pull' ) ;
2646
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2647
+ clock . setTimeout ( ( ) => {
2648
+ resolve ( ) ;
2649
+ } , 3 ) ;
2650
+ } ) ) ;
2651
+
2652
+ const pushStub = sinon . stub ( SyncEngineLevel . prototype as any , 'push' ) ;
2653
+ pushStub . returns ( new Promise < void > ( ( resolve ) => {
2654
+ clock . setTimeout ( ( ) => {
2655
+ resolve ( ) ;
2656
+ } , 3 ) ;
2657
+ } ) ) ;
2658
+
2659
+ testHarness . agent . sync . startSync ( { interval : '500ms' } ) ;
2660
+
2661
+ // expect the immediate sync call
2662
+ expect ( syncSpy . callCount ) . to . equal ( 1 ) ;
2663
+
2664
+ await clock . tickAsync ( 1_300 ) ; // just under 3 intervals
2665
+
2666
+ // expect 2 sync interval calls + initial sync
2667
+ expect ( syncSpy . callCount ) . to . equal ( 3 ) ;
2668
+
2669
+ // cause pull to take longer
2670
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2671
+ clock . setTimeout ( ( ) => {
2672
+ resolve ( ) ;
2673
+ } , 1_000 ) ;
2674
+ } ) ) ;
2675
+
2676
+ await clock . tickAsync ( 201 ) ; // Enough time for the next interval to start
2677
+
2678
+ // next interval was called
2679
+ expect ( syncSpy . callCount ) . to . equal ( 4 ) ;
2680
+
2681
+ // stop the sync
2682
+ await new Promise < void > ( ( resolve ) => {
2683
+ const stopPromise = testHarness . agent . sync . stopSync ( ) ;
2684
+ clock . tickAsync ( 1_000 ) . then ( async ( ) => {
2685
+ await stopPromise ;
2686
+ resolve ( ) ;
2687
+ } ) ;
2688
+ } ) ;
2689
+
2690
+ // sync calls remain unchanged
2691
+ expect ( syncSpy . callCount ) . to . equal ( 4 ) ;
2692
+
2693
+ // wait for future intervals
2694
+ await clock . tickAsync ( 2_000 ) ;
2695
+
2696
+ // sync calls remain unchanged
2697
+ expect ( syncSpy . callCount ) . to . equal ( 4 ) ;
2698
+
2699
+ syncSpy . restore ( ) ;
2700
+ clock . restore ( ) ;
2701
+ } ) ;
2702
+
2703
+ it ( 'throws if ongoing sync does not complete within 2 seconds' , async ( ) => {
2704
+ await testHarness . agent . sync . registerIdentity ( {
2705
+ did : alice . did . uri ,
2706
+ } ) ;
2707
+
2708
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2709
+
2710
+ const syncSpy = sinon . spy ( SyncEngineLevel . prototype as any , 'sync' ) ;
2711
+
2712
+ // stub push and pull to take 3 ms each
2713
+ const pullStub = sinon . stub ( SyncEngineLevel . prototype as any , 'pull' ) ;
2714
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2715
+ clock . setTimeout ( ( ) => {
2716
+ resolve ( ) ;
2717
+ } , 3 ) ;
2718
+ } ) ) ;
2719
+
2720
+ const pushStub = sinon . stub ( SyncEngineLevel . prototype as any , 'push' ) ;
2721
+ pushStub . returns ( new Promise < void > ( ( resolve ) => {
2722
+ clock . setTimeout ( ( ) => {
2723
+ resolve ( ) ;
2724
+ } , 3 ) ;
2725
+ } ) ) ;
2726
+
2727
+ testHarness . agent . sync . startSync ( { interval : '500ms' } ) ;
2728
+
2729
+ // expect the immediate sync call
2730
+ expect ( syncSpy . callCount ) . to . equal ( 1 ) ;
2731
+
2732
+ await clock . tickAsync ( 1_300 ) ; // just under 3 intervals
2733
+
2734
+ // expect 2 sync interval calls + initial sync
2735
+ expect ( syncSpy . callCount ) . to . equal ( 3 ) ;
2736
+
2737
+ // cause pull to take longer
2738
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2739
+ clock . setTimeout ( ( ) => {
2740
+ resolve ( ) ;
2741
+ } , 2_700 ) ; // longer than the 2 seconds
2742
+ } ) ) ;
2743
+
2744
+ await clock . tickAsync ( 201 ) ; // Enough time for the next interval to start
2745
+
2746
+ // next interval was called
2747
+ expect ( syncSpy . callCount ) . to . equal ( 4 ) ;
2748
+
2749
+ const stopPromise = testHarness . agent . sync . stopSync ( ) ;
2750
+
2751
+ try {
2752
+ await new Promise < void > ( ( resolve , reject ) => {
2753
+ stopPromise . catch ( ( error ) => reject ( error ) ) ;
2754
+
2755
+ clock . runToLastAsync ( ) . then ( async ( ) => {
2756
+ try {
2757
+ await stopPromise ;
2758
+ resolve ( ) ;
2759
+ } catch ( error ) {
2760
+ reject ( error ) ;
2761
+ }
2762
+ } ) ;
2763
+
2764
+ } ) ;
2765
+ expect . fail ( 'Expected an error to be thrown' ) ;
2766
+ } catch ( error :any ) {
2767
+ expect ( error . message ) . to . equal ( 'SyncEngineLevel: Existing sync operation did not complete within 2000 milliseconds.' ) ;
2768
+ }
2769
+
2770
+ syncSpy . restore ( ) ;
2771
+ clock . restore ( ) ;
2772
+ } ) ;
2773
+
2774
+ it ( 'only waits for the ongoing sync for the given timeout before failing' , async ( ) => {
2775
+ await testHarness . agent . sync . registerIdentity ( {
2776
+ did : alice . did . uri ,
2777
+ } ) ;
2778
+
2779
+ const clock = sinon . useFakeTimers ( { shouldClearNativeTimers : true } ) ;
2780
+
2781
+ const syncSpy = sinon . spy ( SyncEngineLevel . prototype as any , 'sync' ) ;
2782
+
2783
+ // stub push and pull to take 3 ms each
2784
+ const pullStub = sinon . stub ( SyncEngineLevel . prototype as any , 'pull' ) ;
2785
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2786
+ clock . setTimeout ( ( ) => {
2787
+ resolve ( ) ;
2788
+ } , 3 ) ;
2789
+ } ) ) ;
2790
+
2791
+ const pushStub = sinon . stub ( SyncEngineLevel . prototype as any , 'push' ) ;
2792
+ pushStub . returns ( new Promise < void > ( ( resolve ) => {
2793
+ clock . setTimeout ( ( ) => {
2794
+ resolve ( ) ;
2795
+ } , 3 ) ;
2796
+ } ) ) ;
2797
+
2798
+ testHarness . agent . sync . startSync ( { interval : '500ms' } ) ;
2799
+
2800
+ // expect the immediate sync call
2801
+ expect ( syncSpy . callCount ) . to . equal ( 1 ) ;
2802
+
2803
+ await clock . tickAsync ( 10 ) ; // enough time for the sync round trip to complete
2804
+
2805
+ // cause pull to take longer
2806
+ pullStub . returns ( new Promise < void > ( ( resolve ) => {
2807
+ clock . setTimeout ( ( ) => {
2808
+ resolve ( ) ;
2809
+ } , 2_700 ) ; // longer than the 2 seconds
2810
+ } ) ) ;
2811
+
2812
+ await clock . tickAsync ( 501 ) ; // Enough time for the next interval to start
2813
+
2814
+ // next interval was called
2815
+ expect ( syncSpy . callCount ) . to . equal ( 2 ) ;
2816
+
2817
+ const stopPromise = testHarness . agent . sync . stopSync ( 10 ) ;
2818
+ try {
2819
+ await new Promise < void > ( ( resolve , reject ) => {
2820
+ stopPromise . catch ( ( error ) => reject ( error ) ) ;
2821
+
2822
+ clock . tickAsync ( 10 ) . then ( async ( ) => {
2823
+ try {
2824
+ await stopPromise ;
2825
+ resolve ( ) ;
2826
+ } catch ( error ) {
2827
+ reject ( error ) ;
2828
+ }
2829
+ } ) ;
2830
+
2831
+ } ) ;
2832
+ expect . fail ( 'Expected an error to be thrown' ) ;
2833
+ } catch ( error :any ) {
2834
+ expect ( error . message ) . to . equal ( 'SyncEngineLevel: Existing sync operation did not complete within 10 milliseconds.' ) ;
2835
+ }
2836
+
2837
+ // call again with a longer timeout
2838
+ await new Promise < void > ( ( resolve ) => {
2839
+ const stopPromise2 = testHarness . agent . sync . stopSync ( 3_000 ) ;
2840
+ // enough time for the ongoing sync to complete + 100ms as the check interval
2841
+ clock . tickAsync ( 2800 ) . then ( async ( ) => {
2842
+ stopPromise2 . then ( ( ) => resolve ( ) ) ;
2843
+ } ) ;
2844
+ } ) ;
2845
+
2846
+ await clock . runToLastAsync ( ) ;
2847
+ syncSpy . restore ( ) ;
2848
+ clock . restore ( ) ;
2849
+ } ) ;
2850
+
2851
+ } ) ;
2589
2852
} ) ;
2590
2853
} ) ;
0 commit comments