|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package managedwriter |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +func TestAppendResult(t *testing.T) { |
| 25 | + |
| 26 | + wantRowBytes := []byte("rowdata") |
| 27 | + |
| 28 | + gotAR := newAppendResult(wantRowBytes) |
| 29 | + if !bytes.Equal(gotAR.rowData, wantRowBytes) { |
| 30 | + t.Errorf("mismatch in row data, got %q want %q", gotAR.rowData, wantRowBytes) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestPendingWrite(t *testing.T) { |
| 35 | + wantRowData := [][]byte{ |
| 36 | + []byte("row1"), |
| 37 | + []byte("row2"), |
| 38 | + []byte("row3"), |
| 39 | + } |
| 40 | + |
| 41 | + var wantOffset int64 = 99 |
| 42 | + |
| 43 | + // first, verify no offset behavior |
| 44 | + pending := newPendingWrite(wantRowData, NoStreamOffset) |
| 45 | + if pending.request.GetOffset() != nil { |
| 46 | + t.Errorf("request should have no offset, but is present: %q", pending.request.GetOffset().GetValue()) |
| 47 | + } |
| 48 | + pending.markDone(NoStreamOffset, nil) |
| 49 | + for k, ar := range pending.results { |
| 50 | + if ar.offset != NoStreamOffset { |
| 51 | + t.Errorf("mismatch on completed AppendResult(%d) without offset: got %d want %d", k, ar.offset, NoStreamOffset) |
| 52 | + } |
| 53 | + if ar.err != nil { |
| 54 | + t.Errorf("mismatch in error on AppendResult(%d), got %v want nil", k, ar.err) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // now, verify behavior with a valid offset |
| 59 | + pending = newPendingWrite(wantRowData, 99) |
| 60 | + if pending.request.GetOffset() == nil { |
| 61 | + t.Errorf("offset not set, should be %d", wantOffset) |
| 62 | + } |
| 63 | + if gotOffset := pending.request.GetOffset().GetValue(); gotOffset != wantOffset { |
| 64 | + t.Errorf("offset mismatch, got %d want %d", gotOffset, wantOffset) |
| 65 | + } |
| 66 | + |
| 67 | + // check request shape |
| 68 | + gotRowCount := len(pending.request.GetProtoRows().GetRows().GetSerializedRows()) |
| 69 | + if gotRowCount != len(wantRowData) { |
| 70 | + t.Errorf("pendingWrite request mismatch, got %d rows, want %d rows", gotRowCount, len(wantRowData)) |
| 71 | + } |
| 72 | + |
| 73 | + // verify child AppendResults |
| 74 | + if len(pending.results) != len(wantRowData) { |
| 75 | + t.Errorf("mismatch in rows and append results. %d rows, %d AppendResults", len(wantRowData), len(pending.results)) |
| 76 | + } |
| 77 | + for k, ar := range pending.results { |
| 78 | + gotData := ar.rowData |
| 79 | + if !bytes.Equal(gotData, wantRowData[k]) { |
| 80 | + t.Errorf("row %d mismatch in data: got %q want %q", k, gotData, wantRowData[k]) |
| 81 | + } |
| 82 | + select { |
| 83 | + case <-ar.Ready(): |
| 84 | + t.Errorf("got Ready() on incomplete AppendResult %d", k) |
| 85 | + case <-time.After(100 * time.Millisecond): |
| 86 | + continue |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // verify completion behavior |
| 91 | + reportedOffset := int64(101) |
| 92 | + wantErr := fmt.Errorf("foo") |
| 93 | + pending.markDone(reportedOffset, wantErr) |
| 94 | + |
| 95 | + if pending.request != nil { |
| 96 | + t.Errorf("expected request to be cleared, is present: %#v", pending.request) |
| 97 | + } |
| 98 | + for k, ar := range pending.results { |
| 99 | + gotData := ar.rowData |
| 100 | + if !bytes.Equal(gotData, wantRowData[k]) { |
| 101 | + t.Errorf("row %d mismatch in data: got %q want %q", k, gotData, wantRowData[k]) |
| 102 | + } |
| 103 | + select { |
| 104 | + case <-ar.Ready(): |
| 105 | + continue |
| 106 | + case <-time.After(100 * time.Millisecond): |
| 107 | + t.Errorf("possible blocking on completed AppendResult %d", k) |
| 108 | + } |
| 109 | + if ar.offset != reportedOffset+int64(k) { |
| 110 | + t.Errorf("mismatch on completed AppendResult offset: got %d want %d", ar.offset, reportedOffset+int64(k)) |
| 111 | + } |
| 112 | + if ar.err != wantErr { |
| 113 | + t.Errorf("mismatch in errors, got %v want %v", ar.err, wantErr) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments