Nicer fix for flaky Travis
Peter Bourgon
8 years ago
86 | 86 | runtime.Gosched() // yield to flush |
87 | 87 | |
88 | 88 | // Travis is annoying |
89 | by(t, time.Second, | |
90 | func() bool { return buf.String() != "" }, | |
91 | func() { ch <- time.Now(); runtime.Gosched(); time.Sleep(5 * time.Millisecond) }, | |
92 | "buffer never got write+flush", | |
93 | ) | |
89 | check := func() bool { return buf.String() != "" } | |
90 | execute := func() { ch <- time.Now(); runtime.Gosched(); time.Sleep(5 * time.Millisecond) } | |
91 | by(t, time.Second, check, execute, "buffer never got write+flush") | |
94 | 92 | |
95 | 93 | if want, have := fmt.Sprintf("test_statsd_callback_gauge:%f|g\n", value), buf.String(); want != have { |
96 | 94 | t.Errorf("want %q, have %q", want, have) |
115 | 113 | } |
116 | 114 | } |
117 | 115 | |
118 | func by(t *testing.T, d time.Duration, b func() bool, c func(), msg string) { | |
116 | func by(t *testing.T, d time.Duration, check func() bool, execute func(), msg string) { | |
119 | 117 | deadline := time.Now().Add(d) |
120 | for !b() { | |
118 | for !check() { | |
121 | 119 | if time.Now().After(deadline) { |
122 | 120 | t.Fatal(msg) |
123 | 121 | } |
124 | c() | |
122 | execute() | |
125 | 123 | } |
126 | 124 | } |