thetorpedodog commented on a change in pull request #11413: [BEAM-9746] check
for 0 length copies from state
URL: https://github.com/apache/beam/pull/11413#discussion_r408412639
##########
File path: sdks/go/pkg/beam/core/runtime/harness/statemgr_test.go
##########
@@ -258,6 +261,167 @@ func TestStateChannel(t *testing.T) {
}
}
+// TestStateKeyReader validates ordinary Read cases
+func TestStateKeyReader(t *testing.T) {
+ const readLen = 4
+ tests := []struct {
+ name string
+ buflens []int // sizes of the buffers received on the state
channel.
+ numReads int
+ closed bool // tries to read from closed reader
+ noGet bool // tries to read from nil get response reader
+ }{
+ {
+ name: "emptyData",
+ buflens: []int{-1},
+ numReads: 1,
+ }, {
+ name: "singleBufferSingleRead",
+ buflens: []int{readLen},
+ numReads: 2,
+ }, {
+ name: "singleBufferMultipleReads",
+ buflens: []int{2 * readLen},
+ numReads: 3,
+ }, {
+ name: "singleBufferShortRead",
+ buflens: []int{readLen - 1},
+ numReads: 2,
+ }, {
+ name: "multiBuffer",
+ buflens: []int{readLen, readLen},
+ numReads: 3,
+ }, {
+ name: "multiBuffer-short-reads",
+ buflens: []int{readLen - 1, readLen - 1, readLen - 2},
+ numReads: 4,
+ }, {
+ name: "emptyDataFirst", // Shouldn't happen, but
not unreasonable to handle.
+ buflens: []int{-1, readLen, readLen},
+ numReads: 4,
+ }, {
+ name: "emptyDataMid", // Shouldn't happen, but not
unreasonable to handle.
+ buflens: []int{readLen, readLen, -1, readLen},
+ numReads: 5,
+ }, {
+ name: "emptyDataLast", // Shouldn't happen, but not
unreasonable to handle.
+ buflens: []int{readLen, readLen, -1},
+ numReads: 3,
+ }, {
+ name: "emptyDataLast-short",
+ buflens: []int{3*readLen - 2, -1},
+ numReads: 4,
+ }, {
+ name: "closed",
+ buflens: []int{-1, -1},
+ numReads: 1,
+ closed: true,
+ }, {
+ name: "noGet",
+ buflens: []int{-1},
+ numReads: 1,
+ noGet: true,
+ },
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ ctx, cancelFn :=
context.WithCancel(context.Background())
+ ch := &StateChannel{
+ id: "test",
+ requests: make(chan *fnpb.StateRequest),
+ responses: make(map[string]chan<-
*fnpb.StateResponse),
+ cancelFn: cancelFn,
+ DoneCh: ctx.Done(),
+ }
+
+ // Handle the channel behavior asynchronously.
+ go func() {
+ for i := 0; i < len(test.buflens); i++ {
+ token := []byte(strconv.Itoa(i))
+ var buf []byte
+ if test.buflens[i] >= 0 {
+ buf = bytes.Repeat([]byte{42},
test.buflens[i])
+ }
+ // On the last request response pair,
send no token.
+ if i+1 == len(test.buflens) {
+ token = nil
+ }
+
+ req := <-ch.requests
+
+ if test.noGet {
+ ch.responses[req.Id] <-
&fnpb.StateResponse{
+ Id: req.Id,
+ }
+ return
+ }
+
+ ch.responses[req.Id] <-
&fnpb.StateResponse{
+ Id: req.Id,
+ Response:
&fnpb.StateResponse_Get{
+ Get:
&fnpb.StateGetResponse{
+
ContinuationToken: token,
+ Data:
buf,
+ },
+ },
+ }
+ }
+ }()
+
+ r := stateKeyReader{
+ ch: ch,
+ }
+
+ if test.closed {
+ err := r.Close()
+ if err != nil {
+ t.Errorf("unexpected error on Close(),
got %v", err)
+ }
+ }
+
+ var totalBytes int
+ for _, l := range test.buflens {
+ if l > 0 {
+ totalBytes += l
+ }
+ }
+ var finalerr error
+ var count, reads int
+
+ // Read all the bytes.
+ for count <= totalBytes {
+ reads++
+ b := make([]byte, readLen) // io.Read is keyed
off of length, not capacity.
+ n, err := r.Read(b)
+ if err != nil {
+ finalerr = err
+ break
+ }
+ count += n
+ // Special check to avoid spurious zero
elements.
+ if count == totalBytes && n == 0 {
+ t.Error("expected byte count read, last
read is 0, but no EOF")
+ }
+ }
+ if got, want := reads, test.numReads; got != want {
Review comment:
Is this a common idiom in beam testing? Inside Google, I don't think I've
seen reassignment to want/got for things which are already stored in their own
variables. I would expect something like:
```go
if reads != test.numReads {
t.Errorf("read %d times, want %d", reads, test.numReads)
}
```
for simpler tests I have seen things like:
```go
if got := sut.CallSomeFn(params); got != tc.want {
// ...
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services