@@ -5,62 +5,162 @@ import (
55 "time"
66
77 "github.com/launchdarkly/go-test-helpers/v2/testbox"
8+
89 "github.com/stretchr/testify/assert"
910)
1011
1112func TestTryReceive (t * testing.T ) {
1213 ch := make (chan string , 1 )
13- v , ok := TryReceive (ch , time .Millisecond )
14+ v , ok , closed := TryReceive (ch , time .Millisecond )
1415 assert .False (t , ok )
16+ assert .False (t , closed )
1517 assert .Equal (t , "" , v )
1618
1719 ch <- "a"
18- v , ok = TryReceive (ch , time .Millisecond )
20+ v , ok , closed = TryReceive (ch , time .Millisecond )
1921 assert .True (t , ok )
22+ assert .False (t , closed )
2023 assert .Equal (t , "a" , v )
24+
25+ go func () {
26+ close (ch )
27+ }()
28+ v , ok , closed = TryReceive (ch , time .Second )
29+ assert .False (t , ok )
30+ assert .True (t , closed )
31+ assert .Equal (t , "" , v )
2132}
2233
2334func TestRequireValue (t * testing.T ) {
24- result := testbox .SandboxTest ( func (t1 testbox.TestingT ) {
35+ testbox .ShouldFailAndExitEarly ( t , func (t testbox.TestingT ) {
2536 ch := make (chan string , 1 )
26- _ = RequireValue (t1 , ch , time .Millisecond )
27- t .Errorf ("test should have exited early but did not" )
37+ _ = RequireValue (t , ch , time .Millisecond )
2838 })
29- assert .True (t , result .Failed )
3039
3140 ch := make (chan string , 1 )
3241 go func () {
3342 ch <- "a"
3443 }()
3544 v := RequireValue (t , ch , time .Second )
3645 assert .Equal (t , "a" , v )
46+
47+ testbox .ShouldFailAndExitEarly (t , func (t testbox.TestingT ) {
48+ ch := make (chan string , 1 )
49+ go func () {
50+ close (ch )
51+ }()
52+ _ = RequireValue (t , ch , time .Second )
53+ })
3754}
3855
3956func TestAssertNoMoreValues (t * testing.T ) {
4057 ch := make (chan string , 1 )
4158 AssertNoMoreValues (t , ch , time .Millisecond )
4259
43- result := testbox .SandboxTest ( func (t testbox.TestingT ) {
60+ testbox .ShouldFail ( t , func (t testbox.TestingT ) {
4461 ch := make (chan string , 1 )
4562 go func () {
4663 ch <- "a"
4764 }()
4865 AssertNoMoreValues (t , ch , time .Second )
4966 })
50- assert .True (t , result .Failed )
67+
68+ testbox .ShouldFail (t , func (t testbox.TestingT ) {
69+ ch := make (chan string , 1 )
70+ go func () {
71+ close (ch )
72+ }()
73+ AssertNoMoreValues (t , ch , time .Second )
74+ })
5175}
5276
53- func TestRequireNoMoreValues (t * testing.T ) {
77+ func TestAssertChannelClosed (t * testing.T ) {
5478 ch := make (chan string , 1 )
55- AssertNoMoreValues (t , ch , time .Millisecond )
79+ go func () {
80+ close (ch )
81+ }()
82+ AssertChannelClosed (t , ch , time .Second )
83+
84+ testbox .ShouldFail (t , func (t testbox.TestingT ) {
85+ ch := make (chan string , 1 )
86+ AssertChannelClosed (t , ch , time .Millisecond )
87+ })
88+
89+ testbox .ShouldFail (t , func (t testbox.TestingT ) {
90+ ch := make (chan string , 1 )
91+ ch <- "a"
92+ AssertChannelClosed (t , ch , time .Millisecond )
93+ })
94+ }
95+
96+ func TestAssertChannelNotClosed (t * testing.T ) {
97+ testbox .ShouldFail (t , func (t testbox.TestingT ) {
98+ ch := make (chan string , 1 )
99+ go func () {
100+ close (ch )
101+ }()
102+ AssertChannelNotClosed (t , ch , time .Second )
103+ })
104+
105+ ch := make (chan string , 1 )
106+ AssertChannelNotClosed (t , ch , time .Millisecond )
107+
108+ ch <- "a"
109+ AssertChannelNotClosed (t , ch , time .Millisecond )
110+ }
111+
112+ func TestFailureMessages (t * testing.T ) {
113+ result := testbox .SandboxTest (func (t testbox.TestingT ) {
114+ ch := make (chan string , 1 )
115+ _ = RequireValue (t , ch , time .Millisecond , "sorry%s" , "." )
116+ })
117+ if assert .Len (t , result .Failures , 2 ) {
118+ assert .Equal (t , "expected a string value from channel but did not receive one in 1ms" , result .Failures [0 ].Message )
119+ assert .Equal (t , "sorry." , result .Failures [1 ].Message )
120+ }
56121
57- result : = testbox .SandboxTest (func (t1 testbox.TestingT ) {
122+ result = testbox .SandboxTest (func (t testbox.TestingT ) {
58123 ch := make (chan string , 1 )
59124 go func () {
60125 ch <- "a"
61126 }()
62- RequireNoMoreValues (t1 , ch , time .Second )
63- t .Errorf ("test should have exited early but did not" )
127+ AssertNoMoreValues (t , ch , time .Second , "sorry%s" , "." )
128+ })
129+ if assert .Len (t , result .Failures , 2 ) {
130+ assert .Equal (t , "expected no more string values from channel but got one: a" , result .Failures [0 ].Message )
131+ assert .Equal (t , "sorry." , result .Failures [1 ].Message )
132+ }
133+
134+ result = testbox .SandboxTest (func (t testbox.TestingT ) {
135+ ch := make (chan string , 1 )
136+ go func () {
137+ close (ch )
138+ }()
139+ AssertNoMoreValues (t , ch , time .Second , "sorry%s" , "." )
140+ })
141+ if assert .Len (t , result .Failures , 2 ) {
142+ assert .Equal (t , "channel was unexpectedly closed" , result .Failures [0 ].Message )
143+ assert .Equal (t , "sorry." , result .Failures [1 ].Message )
144+ }
145+
146+ result = testbox .SandboxTest (func (t testbox.TestingT ) {
147+ ch := make (chan string , 1 )
148+ AssertChannelClosed (t , ch , time .Millisecond , "sorry%s" , "." )
149+ })
150+ if assert .Len (t , result .Failures , 2 ) {
151+ assert .Equal (t , "expected channel to be closed within 1ms but it was not" , result .Failures [0 ].Message )
152+ assert .Equal (t , "sorry." , result .Failures [1 ].Message )
153+ }
154+
155+ result = testbox .SandboxTest (func (t testbox.TestingT ) {
156+ ch := make (chan string , 1 )
157+ go func () {
158+ close (ch )
159+ }()
160+ AssertChannelNotClosed (t , ch , time .Second , "sorry%s" , "." )
64161 })
65- assert .True (t , result .Failed )
162+ if assert .Len (t , result .Failures , 2 ) {
163+ assert .Equal (t , "channel was unexpectedly closed" , result .Failures [0 ].Message )
164+ assert .Equal (t , "sorry." , result .Failures [1 ].Message )
165+ }
66166}
0 commit comments