| 1 |
// Copyright (c) 2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
) |
| 17 |
|
| 18 |
// A RecvKind allows to match that nothing has been received on a |
| 19 |
// channel or that a channel has been closed when using [Recv] |
| 20 |
// operator. |
| 21 |
type RecvKind = types.RecvKind |
| 22 |
|
| 23 |
const ( |
| 24 |
_ RecvKind = (iota & 1) == 0 |
| 25 |
RecvNothing // nothing read on channel |
| 26 |
RecvClosed // channel closed |
| 27 |
) |
| 28 |
|
| 29 |
type tdRecv struct { |
| 30 |
tdSmugglerBase |
| 31 |
timeout time.Duration |
| 32 |
} |
| 33 |
|
| 34 |
var _ TestDeep = &tdRecv{} |
| 35 |
|
| 36 |
// summary(Recv): checks the value read from a channel |
| 37 |
// input(Recv): chan,ptr(ptr on chan) |
| 38 |
|
| 39 |
// Recv is a smuggler operator. It reads from a channel or a pointer |
| 40 |
// to a channel and compares the read value to expectedValue. |
| 41 |
// |
| 42 |
// expectedValue can be any value including a [TestDeep] operator. It |
| 43 |
// can also be [RecvNothing] to test nothing can be read from the |
| 44 |
// channel or [RecvClosed] to check the channel is closed. |
| 45 |
// |
| 46 |
// If timeout is passed it should be only one item. It means: try to |
| 47 |
// read the channel during this duration to get a value before giving |
| 48 |
// up. If timeout is missing or ≤ 0, it defaults to 0 meaning Recv |
| 49 |
// does not wait for a value but gives up instantly if no value is |
| 50 |
// available on the channel. |
| 51 |
// |
| 52 |
// ch := make(chan int, 6) |
| 53 |
// td.Cmp(t, ch, td.Recv(td.RecvNothing)) // succeeds |
| 54 |
// td.Cmp(t, ch, td.Recv(42)) // fails, nothing to receive |
| 55 |
// // recv(DATA): values differ |
| 56 |
// // got: nothing received on channel |
| 57 |
// // expected: 42 |
| 58 |
// |
| 59 |
// ch <- 42 |
| 60 |
// td.Cmp(t, ch, td.Recv(td.RecvNothing)) // fails, 42 received instead |
| 61 |
// // recv(DATA): values differ |
| 62 |
// // got: 42 |
| 63 |
// // expected: nothing received on channel |
| 64 |
// |
| 65 |
// td.Cmp(t, ch, td.Recv(42)) // fails, nothing to receive anymore |
| 66 |
// // recv(DATA): values differ |
| 67 |
// // got: nothing received on channel |
| 68 |
// // expected: 42 |
| 69 |
// |
| 70 |
// ch <- 666 |
| 71 |
// td.Cmp(t, ch, td.Recv(td.Between(600, 700))) // succeeds |
| 72 |
// |
| 73 |
// close(ch) |
| 74 |
// td.Cmp(t, ch, td.Recv(td.RecvNothing)) // fails as channel is closed |
| 75 |
// // recv(DATA): values differ |
| 76 |
// // got: channel is closed |
| 77 |
// // expected: nothing received on channel |
| 78 |
// |
| 79 |
// td.Cmp(t, ch, td.Recv(td.RecvClosed)) // succeeds |
| 80 |
// |
| 81 |
// Note that for convenience Recv accepts pointer on channel: |
| 82 |
// |
| 83 |
// ch := make(chan int, 6) |
| 84 |
// ch <- 42 |
| 85 |
// td.Cmp(t, &ch, td.Recv(42)) // succeeds |
| 86 |
// |
| 87 |
// Each time Recv is called, it tries to consume one item from the |
| 88 |
// channel, immediately or, if given, before timeout duration. To |
| 89 |
// consume several items in a same [Cmp] call, one can use [All] |
| 90 |
// operator as in: |
| 91 |
// |
| 92 |
// ch := make(chan int, 6) |
| 93 |
// ch <- 1 |
| 94 |
// ch <- 2 |
| 95 |
// ch <- 3 |
| 96 |
// close(ch) |
| 97 |
// td.Cmp(t, ch, td.All( // succeeds |
| 98 |
// td.Recv(1), |
| 99 |
// td.Recv(2), |
| 100 |
// td.Recv(3), |
| 101 |
// td.Recv(td.RecvClosed), |
| 102 |
// )) |
| 103 |
// |
| 104 |
// To check nothing can be received during 100ms on channel ch (if |
| 105 |
// something is received before, including a close, it fails): |
| 106 |
// |
| 107 |
// td.Cmp(t, ch, td.Recv(td.RecvNothing, 100*time.Millisecond)) |
| 108 |
// |
| 109 |
// note that in case of success, the above [Cmp] call always lasts 100ms. |
| 110 |
// |
| 111 |
// To check 42 can be received from channel ch during the next 100ms |
| 112 |
// (if nothing is received during these 100ms or something different |
| 113 |
// from 42, including a close, it fails): |
| 114 |
// |
| 115 |
// td.Cmp(t, ch, td.Recv(42, 100*time.Millisecond)) |
| 116 |
// |
| 117 |
// note that in case of success, the above [Cmp] call lasts less than 100ms. |
| 118 |
// |
| 119 |
// A nil channel is not handled specifically, so it “is never ready |
| 120 |
// for communication” as specification says: |
| 121 |
// |
| 122 |
// var ch chan int |
| 123 |
// td.Cmp(t, ch, td.Recv(td.RecvNothing)) // always succeeds |
| 124 |
// td.Cmp(t, ch, td.Recv(42)) // or any other value, always fails |
| 125 |
// td.Cmp(t, ch, td.Recv(td.RecvClosed)) // always fails |
| 126 |
// |
| 127 |
// so to check if a channel is not nil before reading from it, one can |
| 128 |
// either do: |
| 129 |
// |
| 130 |
// td.Cmp(t, ch, td.All( |
| 131 |
// td.NotNil(), |
| 132 |
// td.Recv(42), |
| 133 |
// )) |
| 134 |
// // or |
| 135 |
// if td.Cmp(t, ch, td.NotNil()) { |
| 136 |
// td.Cmp(t, ch, td.Recv(42)) |
| 137 |
// } |
| 138 |
// |
| 139 |
// TypeBehind method returns the [reflect.Type] of expectedValue, |
| 140 |
// except if expectedValue is a [TestDeep] operator. In this case, it |
| 141 |
// delegates TypeBehind() to the operator. |
| 142 |
// |
| 143 |
// See also [Cap] and [Len]. |
| 144 |
func Recv(expectedValue any, timeout ...time.Duration) TestDeep { |
| 145 |
r := tdRecv{} |
| 146 |
r.tdSmugglerBase = newSmugglerBase(expectedValue, 0) |
| 147 |
|
| 148 |
if !r.isTestDeeper { |
| 149 |
r.expectedValue = reflect.ValueOf(expectedValue) |
| 150 |
} |
| 151 |
|
| 152 |
switch len(timeout) { |
| 153 |
case 0: |
| 154 |
case 1: |
| 155 |
r.timeout = timeout[0] |
| 156 |
default: |
| 157 |
r.err = ctxerr.OpTooManyParams(r.location.Func, "(EXPECTED[, TIMEOUT])") |
| 158 |
} |
| 159 |
return &r |
| 160 |
} |
| 161 |
|
| 162 |
func (r *tdRecv) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 163 |
if r.err != nil { |
| 164 |
return ctx.CollectError(r.err) |
| 165 |
} |
| 166 |
|
| 167 |
switch got.Kind() { |
| 168 |
case reflect.Ptr: |
| 169 |
gotElem := got.Elem() |
| 170 |
if !gotElem.IsValid() { |
| 171 |
if ctx.BooleanError { |
| 172 |
return ctxerr.BooleanError |
| 173 |
} |
| 174 |
return ctx.CollectError(ctxerr.NilPointer(got, "non-nil *chan")) |
| 175 |
} |
| 176 |
if gotElem.Kind() != reflect.Chan { |
| 177 |
break |
| 178 |
} |
| 179 |
got = gotElem |
| 180 |
fallthrough |
| 181 |
|
| 182 |
case reflect.Chan: |
| 183 |
cases := [2]reflect.SelectCase{ |
| 184 |
{ |
| 185 |
Dir: reflect.SelectRecv, |
| 186 |
Chan: got, |
| 187 |
}, |
| 188 |
} |
| 189 |
var timer *time.Timer |
| 190 |
if r.timeout > 0 { |
| 191 |
timer = time.NewTimer(r.timeout) |
| 192 |
cases[1] = reflect.SelectCase{ |
| 193 |
Dir: reflect.SelectRecv, |
| 194 |
Chan: reflect.ValueOf(timer.C), |
| 195 |
} |
| 196 |
} else { |
| 197 |
cases[1] = reflect.SelectCase{ |
| 198 |
Dir: reflect.SelectDefault, |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
chosen, recv, recvOK := reflect.Select(cases[:]) |
| 203 |
if chosen == 1 && timer != nil { |
| 204 |
// check quickly both timeout & expected case didn't occur |
| 205 |
// concurrently and timeout masked the expected case |
| 206 |
cases[1] = reflect.SelectCase{ |
| 207 |
Dir: reflect.SelectDefault, |
| 208 |
} |
| 209 |
chosen, recv, recvOK = reflect.Select(cases[:]) |
| 210 |
} |
| 211 |
if chosen == 0 { |
| 212 |
if !recvOK { |
| 213 |
recv = reflect.ValueOf(RecvClosed) |
| 214 |
} |
| 215 |
if timer != nil { |
| 216 |
timer.Stop() |
| 217 |
} |
| 218 |
} else { |
| 219 |
recv = reflect.ValueOf(RecvNothing) |
| 220 |
} |
| 221 |
|
| 222 |
return deepValueEqual(ctx.AddFunctionCall("recv"), recv, r.expectedValue) |
| 223 |
} |
| 224 |
|
| 225 |
if ctx.BooleanError { |
| 226 |
return ctxerr.BooleanError |
| 227 |
} |
| 228 |
return ctx.CollectError(ctxerr.BadKind(got, "chan OR *chan")) |
| 229 |
} |
| 230 |
|
| 231 |
func (r *tdRecv) HandleInvalid() bool { |
| 232 |
return true // Knows how to handle untyped nil values (aka invalid values) |
| 233 |
} |
| 234 |
|
| 235 |
func (r *tdRecv) String() string { |
| 236 |
if r.err != nil { |
| 237 |
return r.stringError() |
| 238 |
} |
| 239 |
if r.isTestDeeper { |
| 240 |
return "recv: " + r.expectedValue.Interface().(TestDeep).String() |
| 241 |
} |
| 242 |
return fmt.Sprintf("recv=%d", r.expectedValue.Int()) |
| 243 |
} |
| 244 |
|
| 245 |
func (r *tdRecv) TypeBehind() reflect.Type { |
| 246 |
if r.err != nil { |
| 247 |
return nil |
| 248 |
} |
| 249 |
return r.internalTypeBehind() |
| 250 |
} |