-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package v2 | ||
|
||
// condition is a simple, channel-based condition variable. | ||
type condition chan struct{} | ||
|
||
// Signal signals the condition. Waking up a waiting goroutine. | ||
func (c condition) Signal() { | ||
select { | ||
case c <- struct{}{}: | ||
default: | ||
// Already signaled | ||
} | ||
} | ||
|
||
// C returns the channel used for waiting. | ||
func (c condition) C() <-chan struct{} { | ||
return c | ||
} | ||
|
||
func newCondition() condition { | ||
return make(chan struct{}, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package v2 | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCondition(t *testing.T) { | ||
const timeout = time.Second | ||
t.Run("don't fire until signaled", func(t *testing.T) { | ||
c := newCondition() | ||
wait(t, &c, false, timeout) | ||
}) | ||
t.Run("fire after signaled", func(t *testing.T) { | ||
c := newCondition() | ||
c.Signal() | ||
wait(t, &c, true, timeout) | ||
}) | ||
t.Run("single fire", func(t *testing.T) { | ||
c := newCondition() | ||
c.Signal() | ||
c.Signal() | ||
wait(t, &c, true, timeout) | ||
wait(t, &c, false, timeout) | ||
}) | ||
t.Run("single fire", func(t *testing.T) { | ||
c := newCondition() | ||
c.Signal() | ||
c.Signal() | ||
wait(t, &c, true, timeout) | ||
wait(t, &c, false, timeout) | ||
}) | ||
t.Run("goroutine", func(t *testing.T) { | ||
c := newCondition() | ||
go func() { | ||
c.Signal() | ||
}() | ||
wait(t, &c, true, timeout) | ||
}) | ||
t.Run("loop", func(t *testing.T) { | ||
const iterations = 100 | ||
a, b := newCondition(), newCondition() | ||
go func() { | ||
for i := 0; i < iterations; i++ { | ||
a.Signal() | ||
wait(t, &b, true, timeout) | ||
} | ||
}() | ||
for i := 0; i < iterations; i++ { | ||
wait(t, &a, true, timeout) | ||
b.Signal() | ||
} | ||
}) | ||
} | ||
|
||
func wait(t *testing.T, c *condition, fire bool, timeout time.Duration) { | ||
tm := time.NewTimer(timeout) | ||
defer tm.Stop() | ||
fired := false | ||
select { | ||
case <-tm.C: | ||
case <-c.C(): | ||
fired = true | ||
} | ||
require.Equal(t, fire, fired) | ||
} |