|
| 1 | +package ldbwriter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "github.com/segmentio/ctlstore/pkg/ldb" |
| 7 | + "github.com/segmentio/ctlstore/pkg/schema" |
| 8 | + "github.com/segmentio/ctlstore/pkg/sqlite" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +/* |
| 14 | + * Simple LDBWriteCallback handler that just stores the changes it gets. |
| 15 | + */ |
| 16 | +type TestUpdateCallbackHandler struct { |
| 17 | + Changes []sqlite.SQLiteWatchChange |
| 18 | +} |
| 19 | + |
| 20 | +func (u *TestUpdateCallbackHandler) LDBWritten(ctx context.Context, data LDBWriteMetadata) { |
| 21 | + // The [:0] slice operation will reuse the underlying array of u.Changes if it's large enough |
| 22 | + // to hold all elements of data.Changes, otherwise it will allocate a new one. |
| 23 | + u.Changes = append(u.Changes[:0], data.Changes...) |
| 24 | +} |
| 25 | + |
| 26 | +func (u *TestUpdateCallbackHandler) UpdateCount() int { |
| 27 | + return len(u.Changes) |
| 28 | +} |
| 29 | + |
| 30 | +func (u *TestUpdateCallbackHandler) Reset() { |
| 31 | + u.Changes = u.Changes[:0] |
| 32 | + return |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * Test strategy: |
| 37 | + * Check how many times we get callbacks while applying DML statements, |
| 38 | + * and how many updates we get per callback. |
| 39 | + */ |
| 40 | +func TestCallbackWriter_ApplyDMLStatement(t *testing.T) { |
| 41 | + // Begin boilerplate |
| 42 | + var err error |
| 43 | + ctx := context.Background() |
| 44 | + var changeBuffer sqlite.SQLChangeBuffer |
| 45 | + dbName := "test_ldb_callback_writer" |
| 46 | + _ = sqlite.RegisterSQLiteWatch(dbName, &changeBuffer) |
| 47 | + |
| 48 | + db, err := sql.Open(dbName, ":memory:") |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("Unexpected error: %+v", err) |
| 51 | + } |
| 52 | + defer db.Close() |
| 53 | + |
| 54 | + err = ldb.EnsureLdbInitialized(ctx, db) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("Couldn't initialize SQLite db, error %v", err) |
| 57 | + } |
| 58 | + // End boilerplate |
| 59 | + |
| 60 | + // Set up the callback writer with our test callback handler |
| 61 | + ldbWriteCallback := &TestUpdateCallbackHandler{} |
| 62 | + |
| 63 | + writer := CallbackWriter{ |
| 64 | + DB: db, |
| 65 | + Delegate: &SqlLdbWriter{Db: db}, |
| 66 | + Callbacks: []LDBWriteCallback{ldbWriteCallback}, |
| 67 | + ChangeBuffer: &changeBuffer, |
| 68 | + } |
| 69 | + |
| 70 | + err = writer.ApplyDMLStatement(ctx, schema.NewTestDMLStatement("CREATE TABLE foo (bar VARCHAR);")) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("Could not issue CREATE TABLE statements, error %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + type args struct { |
| 76 | + ctx context.Context |
| 77 | + statements []schema.DMLStatement |
| 78 | + } |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + args args |
| 82 | + expectedCallbacks int |
| 83 | + expectedUpdatesPerCallback int |
| 84 | + wantErr bool |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "Test 1", |
| 88 | + args: args{ |
| 89 | + ctx: ctx, |
| 90 | + statements: []schema.DMLStatement{schema.NewTestDMLStatement("INSERT INTO foo VALUES('dummy');")}, |
| 91 | + }, |
| 92 | + expectedCallbacks: 1, |
| 93 | + expectedUpdatesPerCallback: 1, |
| 94 | + wantErr: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Test 2", |
| 98 | + args: args{ |
| 99 | + ctx: ctx, |
| 100 | + statements: []schema.DMLStatement{ |
| 101 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('boston');"), |
| 102 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('detroit');"), |
| 103 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('chicago');"), |
| 104 | + }, |
| 105 | + }, |
| 106 | + // bare statements outside of a transaction should get a callback each time |
| 107 | + expectedCallbacks: 3, |
| 108 | + expectedUpdatesPerCallback: 1, |
| 109 | + wantErr: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Test 3", |
| 113 | + args: args{ |
| 114 | + ctx: ctx, |
| 115 | + statements: []schema.DMLStatement{ |
| 116 | + schema.NewTestDMLStatement(schema.DMLTxBeginKey), |
| 117 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('asdf');"), |
| 118 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('foo');"), |
| 119 | + schema.NewTestDMLStatement("INSERT INTO foo VALUES('bar');"), |
| 120 | + schema.NewTestDMLStatement(schema.DMLTxEndKey), |
| 121 | + }, |
| 122 | + }, |
| 123 | + // since it's a transaction, we expect only one callback, and it should have all 3 updates |
| 124 | + expectedCallbacks: 1, |
| 125 | + expectedUpdatesPerCallback: 3, |
| 126 | + wantErr: false, |
| 127 | + }, |
| 128 | + } |
| 129 | + for _, tt := range tests { |
| 130 | + t.Run(tt.name, func(t *testing.T) { |
| 131 | + callbackCount := 0 |
| 132 | + for _, statement := range tt.args.statements { |
| 133 | + if err := writer.ApplyDMLStatement(tt.args.ctx, statement); (err != nil) != tt.wantErr { |
| 134 | + t.Errorf("ApplyDMLStatement() error = %v, wantErr %v", err, tt.wantErr) |
| 135 | + } |
| 136 | + // did we get a callback from that statement being applied? |
| 137 | + if ldbWriteCallback.UpdateCount() > 0 { |
| 138 | + callbackCount++ |
| 139 | + assert.Equal(t, tt.expectedUpdatesPerCallback, ldbWriteCallback.UpdateCount()) |
| 140 | + // delete previous callback's update entries since we "handled" the callback |
| 141 | + ldbWriteCallback.Reset() |
| 142 | + } |
| 143 | + } |
| 144 | + assert.Equal(t, tt.expectedCallbacks, callbackCount) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments