github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/times_test.go (about) 1 package f_test 2 3 import ( 4 "github.com/angenalZZZ/gofunc/f" 5 "testing" 6 "time" 7 ) 8 9 func TestAt_Future(test *testing.T) { 10 start := time.Now() 11 done := f.At(start.Add(100*time.Millisecond), func() { 12 diff := time.Now().Sub(start) 13 if diff > 105*time.Millisecond { 14 test.Errorf("Expected to run in 100 ms, it did in %v", diff) 15 } 16 }) 17 <-done 18 } 19 20 func TestAt_Past(test *testing.T) { 21 start := time.Now() 22 done := f.At(start.Add(-100*time.Millisecond), func() {}) 23 <-done 24 diff := time.Now().Sub(start) 25 if diff > time.Millisecond { 26 test.Errorf("Expected to return immediately, but it took %v", diff) 27 } 28 } 29 30 func TestAfter_Future(test *testing.T) { 31 start := time.Now() 32 done := f.After(100*time.Millisecond, func() { 33 diff := time.Now().Sub(start) 34 if diff > 105*time.Millisecond { 35 test.Errorf("Expected to run in 100 ms, it did in %v", diff) 36 } 37 }) 38 <-done 39 } 40 41 func TestEvery(test *testing.T) { 42 dur := 10 * time.Millisecond 43 count := 0 44 f.Every(dur, func() { 45 count++ 46 }) 47 <-time.After(100 * time.Millisecond) 48 count++ 49 if count < 9 { 50 test.Errorf("Expected to run in at least 9 times, it did %v times", count) 51 } 52 } 53 54 func TestUntil_Future(test *testing.T) { 55 count := 0 56 done := f.Until(time.Now().Add(100*time.Millisecond), 10*time.Millisecond, func() { 57 count++ 58 }) 59 <-done 60 if count < 9 { 61 test.Errorf("Expected to run for at least for 9 times, but it ran for %v times", count) 62 } 63 } 64 65 func TestUntil_Past(test *testing.T) { 66 count := 0 67 done := f.Until(time.Now().Add(-100*time.Millisecond), 10*time.Millisecond, func() { 68 count++ 69 }) 70 <-done 71 if count != 0 { 72 test.Errorf("Expected to run for at least for 0 times, but it ran for %v times", count) 73 } 74 } 75 76 func TestTimeout_TimedOut(test *testing.T) { 77 done := f.Timeout(100*time.Millisecond, func() { 78 time.Sleep(time.Minute) 79 }) 80 if <-done { 81 test.Errorf("Expected to get timed out, but it has been completed") 82 } 83 } 84 85 func TestTimeout_Completed(test *testing.T) { 86 done := f.Timeout(time.Minute, func() { 87 time.Sleep(100 * time.Millisecond) 88 }) 89 if !<-done { 90 test.Errorf("Expected to get completed, but it has been timed out") 91 } 92 } 93 94 func TestAll(test *testing.T) { 95 start := time.Now() 96 var val1, val2, val3 bool 97 done := f.All(func() { 98 val1 = true 99 time.Sleep(100 * time.Millisecond) 100 }, func() { 101 val2 = true 102 time.Sleep(100 * time.Millisecond) 103 }, func() { 104 val3 = true 105 time.Sleep(100 * time.Millisecond) 106 }) 107 <-done 108 diff := time.Now().Sub(start) 109 if diff > 105*time.Millisecond { 110 test.Errorf("All takes too long to complete") 111 } 112 if !(val1 && val2 && val3) { 113 test.Errorf("Expected all to run, but at least one didn't") 114 } 115 } 116 117 func TestAllWithThrottle(test *testing.T) { 118 start := time.Now() 119 fn := func() { 120 time.Sleep(100 * time.Millisecond) 121 } 122 done := f.AllWithThrottle(3, fn, fn, fn, fn, fn) 123 <-done 124 diff := time.Now().Sub(start) 125 if diff > 205*time.Millisecond { 126 test.Errorf("All with throttle takes too long to complete") 127 } 128 if diff < 105*time.Millisecond { 129 test.Errorf("All with throttle doesn't take long, throttling may not work") 130 } 131 } 132 133 func TestReplicate(test *testing.T) { 134 results := make(chan bool, 5) 135 done := f.Replicate(5, func() { 136 results <- true 137 }) 138 <-done 139 close(results) 140 count := 0 141 for _ = range results { 142 count++ 143 } 144 if count != 5 { 145 test.Errorf("Expected 5 to run, but %v worked", count) 146 } 147 }