|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + clientapi "github.com/machbase/neo-client/api" |
| 10 | + "github.com/machbase/neo-server/v8/mods/logging" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +type appendWorkerTestConn struct { |
| 15 | + closed int32 |
| 16 | +} |
| 17 | + |
| 18 | +func (c *appendWorkerTestConn) Close() error { |
| 19 | + atomic.AddInt32(&c.closed, 1) |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
| 23 | +func (c *appendWorkerTestConn) Exec(context.Context, string, ...any) clientapi.Result { |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +func (c *appendWorkerTestConn) Query(context.Context, string, ...any) (clientapi.Rows, error) { |
| 28 | + return nil, nil |
| 29 | +} |
| 30 | + |
| 31 | +func (c *appendWorkerTestConn) QueryRow(context.Context, string, ...any) clientapi.Row { |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func (c *appendWorkerTestConn) Prepare(context.Context, string) (clientapi.Stmt, error) { |
| 36 | + return nil, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (c *appendWorkerTestConn) Appender(context.Context, string, ...clientapi.AppenderOption) (clientapi.Appender, error) { |
| 40 | + return nil, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (c *appendWorkerTestConn) Explain(context.Context, string, bool) (string, error) { |
| 44 | + return "", nil |
| 45 | +} |
| 46 | + |
| 47 | +type appendWorkerTestAppender struct { |
| 48 | + tableName string |
| 49 | + tableType clientapi.TableType |
| 50 | + columns clientapi.Columns |
| 51 | + appendRows [][]any |
| 52 | + closed int32 |
| 53 | +} |
| 54 | + |
| 55 | +func (a *appendWorkerTestAppender) TableName() string { |
| 56 | + return a.tableName |
| 57 | +} |
| 58 | + |
| 59 | +func (a *appendWorkerTestAppender) Append(values ...any) error { |
| 60 | + a.appendRows = append(a.appendRows, values) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (a *appendWorkerTestAppender) AppendLogTime(ts time.Time, values ...any) error { |
| 65 | + row := append([]any{ts}, values...) |
| 66 | + a.appendRows = append(a.appendRows, row) |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (a *appendWorkerTestAppender) Close() (int64, int64, error) { |
| 71 | + atomic.AddInt32(&a.closed, 1) |
| 72 | + return int64(len(a.appendRows)), 0, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (a *appendWorkerTestAppender) Columns() (clientapi.Columns, error) { |
| 76 | + return a.columns, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (a *appendWorkerTestAppender) TableType() clientapi.TableType { |
| 80 | + return a.tableType |
| 81 | +} |
| 82 | + |
| 83 | +func (a *appendWorkerTestAppender) WithInputColumns(...string) clientapi.Appender { |
| 84 | + return a |
| 85 | +} |
| 86 | + |
| 87 | +func (a *appendWorkerTestAppender) WithInputFormats(...string) clientapi.Appender { |
| 88 | + return a |
| 89 | +} |
| 90 | + |
| 91 | +func (a *appendWorkerTestAppender) WithBatchMaxRows(int) clientapi.Appender { |
| 92 | + return a |
| 93 | +} |
| 94 | + |
| 95 | +func (a *appendWorkerTestAppender) WithBatchMaxBytes(int) clientapi.Appender { |
| 96 | + return a |
| 97 | +} |
| 98 | + |
| 99 | +func (a *appendWorkerTestAppender) WithBatchMaxDelay(time.Duration) clientapi.Appender { |
| 100 | + return a |
| 101 | +} |
| 102 | + |
| 103 | +func newAppendWorkerForTest(tableName string) (*AppendWorker, *appendWorkerTestAppender, *appendWorkerTestConn) { |
| 104 | + ctx, cancel := context.WithCancel(context.Background()) |
| 105 | + appender := &appendWorkerTestAppender{ |
| 106 | + tableName: tableName, |
| 107 | + tableType: clientapi.TableTypeLog, |
| 108 | + columns: clientapi.Columns{ |
| 109 | + {Name: "NAME", DataType: clientapi.DataTypeString}, |
| 110 | + {Name: "VALUE", DataType: clientapi.DataTypeFloat64}, |
| 111 | + }, |
| 112 | + } |
| 113 | + conn := &appendWorkerTestConn{} |
| 114 | + return &AppendWorker{ |
| 115 | + ctx: ctx, |
| 116 | + ctxCancel: cancel, |
| 117 | + conn: conn, |
| 118 | + appender: appender, |
| 119 | + tableDesc: &clientapi.TableDescription{Name: tableName}, |
| 120 | + lastTime: time.Now(), |
| 121 | + log: logging.GetLog("append-worker-test"), |
| 122 | + }, appender, conn |
| 123 | +} |
| 124 | + |
| 125 | +func TestAppendWorkerRegistryStopsByLowerCaseName(t *testing.T) { |
| 126 | + StartAppendWorkers() |
| 127 | + t.Cleanup(StopAppendWorkers) |
| 128 | + |
| 129 | + worker, appender, conn := newAppendWorkerForTest("sensor") |
| 130 | + appendersLock.Lock() |
| 131 | + appenders["sensor"] = worker |
| 132 | + appendersLock.Unlock() |
| 133 | + |
| 134 | + ack := StopAppendWorker("SENSOR") |
| 135 | + select { |
| 136 | + case <-ack: |
| 137 | + case <-time.After(time.Second): |
| 138 | + t.Fatal("timed out waiting for append worker stop ack") |
| 139 | + } |
| 140 | + |
| 141 | + appendersLock.Lock() |
| 142 | + _, exists := appenders["sensor"] |
| 143 | + appendersLock.Unlock() |
| 144 | + require.False(t, exists) |
| 145 | + require.Equal(t, int32(1), atomic.LoadInt32(&appender.closed)) |
| 146 | + require.Equal(t, int32(1), atomic.LoadInt32(&conn.closed)) |
| 147 | +} |
| 148 | + |
| 149 | +func TestFlushAppendWorkersMatchesNamesCaseInsensitively(t *testing.T) { |
| 150 | + StartAppendWorkers() |
| 151 | + t.Cleanup(StopAppendWorkers) |
| 152 | + |
| 153 | + sensor, sensorAppender, _ := newAppendWorkerForTest("sensor") |
| 154 | + metric, metricAppender, _ := newAppendWorkerForTest("metric") |
| 155 | + appendersLock.Lock() |
| 156 | + appenders["sensor"] = sensor |
| 157 | + appenders["metric"] = metric |
| 158 | + appendersLock.Unlock() |
| 159 | + |
| 160 | + FlushAppendWorkers("SENSOR") |
| 161 | + |
| 162 | + appendersLock.Lock() |
| 163 | + _, sensorExists := appenders["sensor"] |
| 164 | + _, metricExists := appenders["metric"] |
| 165 | + appendersLock.Unlock() |
| 166 | + require.False(t, sensorExists) |
| 167 | + require.True(t, metricExists) |
| 168 | + require.Equal(t, int32(1), atomic.LoadInt32(&sensorAppender.closed)) |
| 169 | + require.Equal(t, int32(0), atomic.LoadInt32(&metricAppender.closed)) |
| 170 | + |
| 171 | + FlushAppendWorkers() |
| 172 | + require.Equal(t, int32(1), atomic.LoadInt32(&metricAppender.closed)) |
| 173 | + require.Empty(t, appenders) |
| 174 | +} |
| 175 | + |
| 176 | +func TestGetAppendWorkerReusesRegisteredWorkerCaseInsensitively(t *testing.T) { |
| 177 | + StartAppendWorkers() |
| 178 | + t.Cleanup(StopAppendWorkers) |
| 179 | + |
| 180 | + worker, _, _ := newAppendWorkerForTest("sensor") |
| 181 | + appendersLock.Lock() |
| 182 | + appenders["sensor"] = worker |
| 183 | + appendersLock.Unlock() |
| 184 | + |
| 185 | + got, err := GetAppendWorker(context.Background(), nil, "SENSOR") |
| 186 | + require.NoError(t, err) |
| 187 | + require.Same(t, worker, got) |
| 188 | + require.Equal(t, int32(1), atomic.LoadInt32(&got.refCount)) |
| 189 | +} |
| 190 | + |
| 191 | +func TestAppenderWithWorkerMapsInputColumns(t *testing.T) { |
| 192 | + worker, _, _ := newAppendWorkerForTest("sensor") |
| 193 | + worker.appendC = make(chan []interface{}, 1) |
| 194 | + |
| 195 | + wrapped := worker.WithInputColumns("value", "name") |
| 196 | + require.NoError(t, wrapped.Append(3.14, "temperature")) |
| 197 | + require.Equal(t, []interface{}{"temperature", 3.14}, <-worker.appendC) |
| 198 | + |
| 199 | + require.EqualError(t, worker.WithInputColumns().Append("only-name"), "value count 1, table 'sensor' requires 2 columns to append") |
| 200 | +} |
| 201 | + |
| 202 | +func TestAppendWorkerAppendLogTimeRequiresLogTable(t *testing.T) { |
| 203 | + worker, appender, _ := newAppendWorkerForTest("sensor") |
| 204 | + worker.appendC = make(chan []interface{}, 1) |
| 205 | + ts := time.Unix(1, 2) |
| 206 | + require.NoError(t, worker.AppendLogTime(ts, "temperature", 3.14)) |
| 207 | + require.Equal(t, []interface{}{ts, "temperature", 3.14}, <-worker.appendC) |
| 208 | + |
| 209 | + appender.tableType = clientapi.TableTypeFixed |
| 210 | + err := worker.AppendLogTime(ts, "temperature", 3.14) |
| 211 | + require.EqualError(t, err, "sensor is not a log table, use Append() instead") |
| 212 | +} |
0 commit comments