github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/mclock/simclock_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package mclock 19 20 import ( 21 "testing" 22 "time" 23 ) 24 25 var _ Clock = System{} 26 var _ Clock = new(Simulated) 27 28 func TestSimulatedAfter(t *testing.T) { 29 const timeout = 30 * time.Minute 30 const adv = time.Minute 31 32 var ( 33 c Simulated 34 end = c.Now().Add(timeout) 35 ch = c.After(timeout) 36 ) 37 for c.Now() < end.Add(-adv) { 38 c.Run(adv) 39 select { 40 case <-ch: 41 t.Fatal("Timer fired early") 42 default: 43 } 44 } 45 46 c.Run(adv) 47 select { 48 case stamp := <-ch: 49 want := time.Time{}.Add(timeout) 50 if !stamp.Equal(want) { 51 t.Errorf("Wrong time sent on timer channel: got %v, want %v", stamp, want) 52 } 53 default: 54 t.Fatal("Timer didn't fire") 55 } 56 } 57 58 func TestSimulatedAfterFunc(t *testing.T) { 59 var c Simulated 60 61 called1 := false 62 timer1 := c.AfterFunc(100*time.Millisecond, func() { called1 = true }) 63 if c.ActiveTimers() != 1 { 64 t.Fatalf("%d active timers, want one", c.ActiveTimers()) 65 } 66 if fired := timer1.Stop(); !fired { 67 t.Fatal("Stop returned false even though timer didn't fire") 68 } 69 if c.ActiveTimers() != 0 { 70 t.Fatalf("%d active timers, want zero", c.ActiveTimers()) 71 } 72 if called1 { 73 t.Fatal("timer 1 called") 74 } 75 if fired := timer1.Stop(); fired { 76 t.Fatal("Stop returned true after timer was already stopped") 77 } 78 79 called2 := false 80 timer2 := c.AfterFunc(100*time.Millisecond, func() { called2 = true }) 81 c.Run(50 * time.Millisecond) 82 if called2 { 83 t.Fatal("timer 2 called") 84 } 85 c.Run(51 * time.Millisecond) 86 if !called2 { 87 t.Fatal("timer 2 not called") 88 } 89 if fired := timer2.Stop(); fired { 90 t.Fatal("Stop returned true after timer has fired") 91 } 92 } 93 94 func TestSimulatedSleep(t *testing.T) { 95 var ( 96 c Simulated 97 timeout = 1 * time.Hour 98 done = make(chan AbsTime) 99 ) 100 go func() { 101 c.Sleep(timeout) 102 done <- c.Now() 103 }() 104 105 c.WaitForTimers(1) 106 c.Run(2 * timeout) 107 select { 108 case stamp := <-done: 109 want := AbsTime(2 * timeout) 110 if stamp != want { 111 t.Errorf("Wrong time after sleep: got %v, want %v", stamp, want) 112 } 113 case <-time.After(5 * time.Second): 114 t.Fatal("Sleep didn't return in time") 115 } 116 }