@@ -50,15 +50,48 @@ var (
5050 }
5151)
5252
53+ // waitWatcherStarts waits up to 1s for the keystore watcher to start.
54+ func waitWatcherStart (ks * KeyStore ) bool {
55+ // On systems where file watch is not supported, just return "ok".
56+ if ! ks .cache .watcher .enabled () {
57+ return true
58+ }
59+ // The watcher should start, and then exit.
60+ for t0 := time .Now (); time .Since (t0 ) < 1 * time .Second ; time .Sleep (100 * time .Millisecond ) {
61+ if ks .cache .watcherStarted () {
62+ return true
63+ }
64+ }
65+ return false
66+ }
67+
68+ func waitForAccounts (wantAccounts []accounts.Account , ks * KeyStore ) error {
69+ var list []accounts.Account
70+ for t0 := time .Now (); time .Since (t0 ) < 5 * time .Second ; time .Sleep (200 * time .Millisecond ) {
71+ list = ks .Accounts ()
72+ if reflect .DeepEqual (list , wantAccounts ) {
73+ // ks should have also received change notifications
74+ select {
75+ case <- ks .changes :
76+ default :
77+ return fmt .Errorf ("wasn't notified of new accounts" )
78+ }
79+ return nil
80+ }
81+ }
82+ return fmt .Errorf ("\n got %v\n want %v" , list , wantAccounts )
83+ }
84+
5385func TestWatchNewFile (t * testing.T ) {
5486 t .Parallel ()
5587
5688 dir , ks := tmpKeyStore (t , false )
5789
5890 // Ensure the watcher is started before adding any files.
5991 ks .Accounts ()
60- time .Sleep (1000 * time .Millisecond )
61-
92+ if ! waitWatcherStart (ks ) {
93+ t .Fatal ("keystore watcher didn't start in time" )
94+ }
6295 // Move in the files.
6396 wantAccounts := make ([]accounts.Account , len (cachetestAccounts ))
6497 for i := range cachetestAccounts {
@@ -72,37 +105,25 @@ func TestWatchNewFile(t *testing.T) {
72105 }
73106
74107 // ks should see the accounts.
75- var list []accounts.Account
76- for d := 200 * time .Millisecond ; d < 5 * time .Second ; d *= 2 {
77- list = ks .Accounts ()
78- if reflect .DeepEqual (list , wantAccounts ) {
79- // ks should have also received change notifications
80- select {
81- case <- ks .changes :
82- default :
83- t .Fatalf ("wasn't notified of new accounts" )
84- }
85- return
86- }
87- time .Sleep (d )
108+ if err := waitForAccounts (wantAccounts , ks ); err != nil {
109+ t .Error (err )
88110 }
89- t .Errorf ("got %s, want %s" , spew .Sdump (list ), spew .Sdump (wantAccounts ))
90111}
91112
92113func TestWatchNoDir (t * testing.T ) {
93114 t .Parallel ()
94-
95115 // Create ks but not the directory that it watches.
96116 rand .Seed (time .Now ().UnixNano ())
97117 dir := filepath .Join (os .TempDir (), fmt .Sprintf ("eth-keystore-watchnodir-test-%d-%d" , os .Getpid (), rand .Int ()))
98118 ks := NewKeyStore (dir , LightScryptN , LightScryptP )
99-
100119 list := ks .Accounts ()
101120 if len (list ) > 0 {
102121 t .Error ("initial account list not empty:" , list )
103122 }
104- time .Sleep (100 * time .Millisecond )
105-
123+ // The watcher should start, and then exit.
124+ if ! waitWatcherStart (ks ) {
125+ t .Fatal ("keystore watcher didn't start in time" )
126+ }
106127 // Create the directory and copy a key file into it.
107128 os .MkdirAll (dir , 0700 )
108129 defer os .RemoveAll (dir )
@@ -295,24 +316,6 @@ func TestCacheFind(t *testing.T) {
295316 }
296317}
297318
298- func waitForAccounts (wantAccounts []accounts.Account , ks * KeyStore ) error {
299- var list []accounts.Account
300- for d := 200 * time .Millisecond ; d < 8 * time .Second ; d *= 2 {
301- list = ks .Accounts ()
302- if reflect .DeepEqual (list , wantAccounts ) {
303- // ks should have also received change notifications
304- select {
305- case <- ks .changes :
306- default :
307- return fmt .Errorf ("wasn't notified of new accounts" )
308- }
309- return nil
310- }
311- time .Sleep (d )
312- }
313- return fmt .Errorf ("\n got %v\n want %v" , list , wantAccounts )
314- }
315-
316319// TestUpdatedKeyfileContents tests that updating the contents of a keystore file
317320// is noticed by the watcher, and the account cache is updated accordingly
318321func TestUpdatedKeyfileContents (t * testing.T ) {
@@ -327,8 +330,9 @@ func TestUpdatedKeyfileContents(t *testing.T) {
327330 if len (list ) > 0 {
328331 t .Error ("initial account list not empty:" , list )
329332 }
330- time .Sleep (100 * time .Millisecond )
331-
333+ if ! waitWatcherStart (ks ) {
334+ t .Fatal ("keystore watcher didn't start in time" )
335+ }
332336 // Create the directory and copy a key file into it.
333337 os .MkdirAll (dir , 0700 )
334338 defer os .RemoveAll (dir )
@@ -346,9 +350,8 @@ func TestUpdatedKeyfileContents(t *testing.T) {
346350 t .Error (err )
347351 return
348352 }
349-
350353 // needed so that modTime of `file` is different to its current value after forceCopyFile
351- time .Sleep (1000 * time .Millisecond )
354+ time .Sleep (time .Second )
352355
353356 // Now replace file contents
354357 if err := forceCopyFile (file , cachetestAccounts [1 ].URL .Path ); err != nil {
@@ -364,7 +367,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
364367 }
365368
366369 // needed so that modTime of `file` is different to its current value after forceCopyFile
367- time .Sleep (1000 * time .Millisecond )
370+ time .Sleep (time .Second )
368371
369372 // Now replace file contents again
370373 if err := forceCopyFile (file , cachetestAccounts [2 ].URL .Path ); err != nil {
@@ -380,7 +383,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
380383 }
381384
382385 // needed so that modTime of `file` is different to its current value after os.WriteFile
383- time .Sleep (1000 * time .Millisecond )
386+ time .Sleep (time .Second )
384387
385388 // Now replace file contents with crap
386389 if err := os .WriteFile (file , []byte ("foo" ), 0600 ); err != nil {
0 commit comments