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_r408403890
##########
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
+ }
Review comment:
rearranging this so that all the initialization for this variable is
together might make it easier to follow:
```go
var buf []byte
if ... {
...
}
// I can see it making sense to reverse the if-statement here to match the
one above,
// but it there is a case to keep it as-is so that the common case is
outside the 'if'.
// Up to you.
var token []byte
if (not last entry) {
// Maybe use fmt.Sprint() here rather than Itoa to avoid needing the
strconv package?
token = []byte(fmt.Sprint(i))
}
ch.responses[...] <- ...
```
----------------------------------------------------------------
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