From b407e3fa696523f2055e3bdba2b12820c5dc42bb Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Thu, 5 Nov 2026 18:10:15 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=93=9D=20docs:=20describe=20the=20pre?= =?UTF-8?q?vious=5Fsdk=20tag=20and=20the=20option=20API=20soak=20calls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/ci.md | 6 ++++++ docs/soak.md | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/ci.md b/docs/ci.md index 4b6fd3d..50bae9d 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -27,6 +27,12 @@ only the current versions can pass goes in a file with a `!previous` build constraint so that the rest keeps running, until the previous pin has that behaviour too. +The SDK's API changed at v0.24.0, so `integration-previous-sdk` also sets the +`previous_sdk` tag. `sdk_current_test.go` and `sdk_previous_test.go` hold the calls +that differ. The soak and benchmark tests use packages built on the current API +only, so they do not run on the previous SDK. When `weir_go_previous` reaches +v0.24.0, delete the tag, `sdk_previous_test.go` and the constraints on those tests. + ## Output All generated output belongs under the ignored `results/` directory, conventionally diff --git a/docs/soak.md b/docs/soak.md index d52bd6d..9708ba0 100644 --- a/docs/soak.md +++ b/docs/soak.md @@ -16,12 +16,12 @@ shares of a backend's workers. The default exercises every call: | Profile | Writes | Reads | | --- | --- | --- | -| `single` | `Put` | `ReadOne` | -| `visible` | query-visible `Put` | `ReadOne` | -| `lua` | `AtomicTransform` | `ReadOne` | -| `batch` | `Mutate` of `-batch-size` keys | `Read` of as many | -| `stream` | a `MutateStream` held for `-stream-lifetime` | a `ReadStream` held as long | -| `native` | `Put` | the backend's own read through `Native` | +| `single` | a `Mutate` of one `MutationPut` | a `Read` of one key | +| `visible` | the same, with `WithQueryVisible` | a `Read` of one key | +| `lua` | a `Mutate` of one `MutationAtomicTransform` | a `Read` of one key | +| `batch` | a `Mutate` of `-batch-size` keys | a `Read` of as many | +| `stream` | a `Mutate` with `WithNext` and `WithMutateConsume`, held for `-stream-lifetime` | a `Read` with `WithNext` and `WithReadConsume`, held as long | +| `native` | a `Mutate` of one `MutationPut` | the backend's own read through `Native` | `-read-percent` chooses between a worker's reads and writes; a worker in the `stream` profile alternates, because each of its turns lasts for many records, From bcbca655a8914776518523cbf923a86f2444c968 Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Thu, 5 Nov 2026 18:10:16 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=91=BD=EF=B8=8F=20fix:=20move=20to=20?= =?UTF-8?q?the=20weir-go=20v0.24.0=20option=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The integration tests reach the SDK through sdk_current_test.go and sdk_previous_test.go, so integration-previous-sdk still runs them on v0.23.1. Soak and benchmark tests need the current API and stay out of that run for one release. A call returns with its last result, before the server may have recorded the stream's end, so stream metrics now wait for that record. --- Makefile | 4 +- go.mod | 2 +- go.sum | 4 +- internal/benchmark/crawler_client.go | 16 +- internal/benchmark/crawler_completion_test.go | 4 +- internal/benchmark/crawler_single_test.go | 2 +- internal/benchmark/partial_update.go | 7 +- internal/benchmark/partial_update_test.go | 2 +- internal/soak/soak.go | 2 +- internal/soak/soak_test.go | 164 ++++++++++-------- internal/soak/worker.go | 121 ++++++++----- internal/workload/paths.go | 19 +- internal/workload/weir_batch.go | 6 +- internal/workload/weir_batch_test.go | 34 ++-- tests/integration/backends_test.go | 21 ++- tests/integration/batch_test.go | 29 ++-- tests/integration/benchmark_test.go | 2 +- tests/integration/lifecycle_test.go | 70 ++++---- tests/integration/native_pagination_test.go | 15 +- .../native_pagination_unit_test.go | 32 ++-- tests/integration/sdk_current_test.go | 36 ++++ tests/integration/sdk_previous_test.go | 70 ++++++++ tests/integration/sdk_test.go | 54 ++++++ tests/integration/semantics_test.go | 56 +++--- tests/integration/silent_test.go | 3 +- tests/integration/single_benchmark_test.go | 2 +- tests/integration/soak_test.go | 2 +- tests/integration/stream_memory_test.go | 19 +- tests/integration/system_test.go | 42 +++-- versions.json | 2 +- 30 files changed, 519 insertions(+), 323 deletions(-) create mode 100644 tests/integration/sdk_current_test.go create mode 100644 tests/integration/sdk_previous_test.go create mode 100644 tests/integration/sdk_test.go diff --git a/Makefile b/Makefile index 3f357fc..d690fa9 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ integration: integration-previous-server: WEIR_TEST_BINARY="$(CURDIR)/.tools/previous/weir" go test -race -tags=integration,previous -count=1 -timeout=10m -v ./tests/integration -# GOFLAGS, not a flag of go test, so that the binaries the tests build use the same SDK. +# GOFLAGS, not a flag of go test, so that any binary a test builds uses the same SDK. # The SDK brings the protocol version it was released with, as it does for a caller. integration-previous-sdk: mkdir -p .tools @@ -27,7 +27,7 @@ integration-previous-sdk: version=$$(jq -er .weir_go_previous versions.json) && \ protocol=$$(go mod download -json github.com/batchstream/weir-go@$$version | jq -er .GoMod | xargs awk '$$1 == "github.com/batchstream/weir-protocol" {print $$2}') && \ go get -modfile=.tools/previous-sdk.mod github.com/batchstream/weir-go@$$version github.com/batchstream/weir-protocol@$$protocol - GOFLAGS=-modfile="$(CURDIR)/.tools/previous-sdk.mod" WEIR_TEST_BINARY="$(CURDIR)/.tools/weir" go test -race -tags=integration,previous -count=1 -timeout=10m -v ./tests/integration + GOFLAGS=-modfile="$(CURDIR)/.tools/previous-sdk.mod" WEIR_TEST_BINARY="$(CURDIR)/.tools/weir" go test -race -tags=integration,previous,previous_sdk -count=1 -timeout=10m -v ./tests/integration benchmark: go run ./cmd/weir-lab -mode saturation -weir "$(CURDIR)/.tools/weir" -backend all -database-cpus 1 -client-processes 4 -concurrency-levels 4,8,32,128,256 -batch-sizes 1 -records 8192 -write-percent 0 -rounds 3 -warmup-duration 10s -duration 20s -output results/local/benchmark diff --git a/go.mod b/go.mod index a33744f..3f10ace 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/batchstream/weir-tests go 1.27.3 require ( - github.com/batchstream/weir-go v0.23.0 + github.com/batchstream/weir-go v0.24.0 github.com/batchstream/weir-protocol v0.17.0 go.mongodb.org/mongo-driver/v2 v2.9.1 google.golang.org/grpc v1.83.2 diff --git a/go.sum b/go.sum index 3e3879a..ef5a890 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/batchstream/weir-go v0.23.0 h1:2PMFV1L2dtVzzORzu+ccwe7TfF3M8VsW96yNaPWtYqA= -github.com/batchstream/weir-go v0.23.0/go.mod h1:tW3APP6fK6A+dVLzwcgkRECIDzlhv0hnhTCyJghcjxw= +github.com/batchstream/weir-go v0.24.0 h1:Ij8r7ceDOvEVduNOfnpSpN7mlUocF2H+e2fsp4ejiTY= +github.com/batchstream/weir-go v0.24.0/go.mod h1:tW3APP6fK6A+dVLzwcgkRECIDzlhv0hnhTCyJghcjxw= github.com/batchstream/weir-protocol v0.17.0 h1:0gfA9nIoRu+6Lhzmtpxv4HruaB4dBDuyAL45x+9OF6g= github.com/batchstream/weir-protocol v0.17.0/go.mod h1:05pTsdu4nJkyZjvdz67jILOiuEicw0Vq88M0ucD4GVY= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= diff --git a/internal/benchmark/crawler_client.go b/internal/benchmark/crawler_client.go index 0f90435..d35ea0f 100644 --- a/internal/benchmark/crawler_client.go +++ b/internal/benchmark/crawler_client.go @@ -204,9 +204,12 @@ func (c *crawlerClient) executeMutations(ctx context.Context, mutations []crawle if err != nil { return false, err } - opts := weir.MutateOptions{StoreName: "search", Requests: requests} + options := []weir.MutateOption{weir.WithRequests(requests...)} + if crawlerQueryVisible(c.config) { + options = append(options, weir.WithQueryVisible()) + } ledger.record(len(requests)) - replies, err := c.sdk.Mutate(ctx, opts) + replies, err := c.sdk.Mutate(ctx, "search", options...) unknown := len(replies) != len(requests) var failures []error if err != nil { @@ -271,6 +274,12 @@ func (c *crawlerClient) executeMutations(ctx context.Context, mutations []crawle return false, nil } +// crawlerQueryVisible reports whether a call waits for query visibility. Only the +// partial-update profile may wait for application acknowledgement instead. +func crawlerQueryVisible(config crawlerClientConfig) bool { + return config.Visibility != "application_acknowledged" +} + func crawlerWeirRequests(config crawlerClientConfig, mutations []crawlerMutation) ([]*weir.MutateRequest, error) { if config.InputShape == "single" && len(mutations) != 1 { return nil, errors.New("single-record SDK call contains a client batch") @@ -298,8 +307,7 @@ func crawlerWeirRequests(config crawlerClientConfig, mutations []crawlerMutation } input := &weir.Document{ContentType: "application/json", Data: raw} program := &weir.LuaTransform{Source: []byte(source), Input: input} - visibility := &weir.MutationOptions{Visibility: weir.MutationQueryVisible} - request := &weir.MutateRequest{Resource: crawlerIndex(config.Namespace, mutation.Product) + "/s:" + mutation.ID, Action: weir.MutationAtomicTransform, Lua: program, Options: visibility} + request := &weir.MutateRequest{Resource: crawlerIndex(config.Namespace, mutation.Product) + "/s:" + mutation.ID, Action: weir.MutationAtomicTransform, Lua: program} requests[index] = request } return requests, nil diff --git a/internal/benchmark/crawler_completion_test.go b/internal/benchmark/crawler_completion_test.go index cd74243..862eca8 100644 --- a/internal/benchmark/crawler_completion_test.go +++ b/internal/benchmark/crawler_completion_test.go @@ -9,8 +9,6 @@ import ( "sync/atomic" "testing" "time" - - weir "github.com/batchstream/weir-go" ) func TestPartialUpdateApplicationAcknowledgedMatchesNativeAndPreservesPadding(t *testing.T) { @@ -47,7 +45,7 @@ func TestPartialUpdateApplicationAcknowledgedMatchesNativeAndPreservesPadding(t if err != nil || len(requests) != 1 { t.Fatal(requests, err) } - if requests[0].Options.Visibility != weir.MutationApplicationAcknowledged || requests[0].Lua != nil { + if crawlerQueryVisible(config) || requests[0].Lua != nil { t.Fatal("Weir did not use the same application completion boundary", requests[0]) } var payload map[string]any diff --git a/internal/benchmark/crawler_single_test.go b/internal/benchmark/crawler_single_test.go index 81fc317..2e9cee6 100644 --- a/internal/benchmark/crawler_single_test.go +++ b/internal/benchmark/crawler_single_test.go @@ -134,7 +134,7 @@ func TestCrawlerSingleSDKConstructsExactlyOneRecordAndRejectsBatches(t *testing. t.Fatal("single SDK call was not one record", requests, err) } request := requests[0] - if request.Options == nil || request.Options.Visibility != weir.MutationQueryVisible || request.Action != weir.MutationAtomicTransform || request.Lua == nil || request.Lua.Input == nil { + if !crawlerQueryVisible(config) || request.Action != weir.MutationAtomicTransform || request.Lua == nil || request.Lua.Input == nil { t.Fatal("single SDK call lost Lua or completion semantics", request) } var actual crawlerMutation diff --git a/internal/benchmark/partial_update.go b/internal/benchmark/partial_update.go index b68b66e..e0fa956 100644 --- a/internal/benchmark/partial_update.go +++ b/internal/benchmark/partial_update.go @@ -44,11 +44,6 @@ func partialUpdateRequest(config crawlerClientConfig, mutation crawlerMutation) return nil, err } expression := &weir.Document{ContentType: "application/vnd.weir.search-update.v1+json", Data: raw} - completion := weir.MutationQueryVisible - if config.Visibility == "application_acknowledged" { - completion = weir.MutationApplicationAcknowledged - } - visibility := &weir.MutationOptions{Visibility: completion} - request := &weir.MutateRequest{Resource: crawlerIndex(config.Namespace, true) + "/s:" + mutation.ID, Action: weir.MutationAtomicTransform, BackendExpression: expression, Options: visibility} + request := &weir.MutateRequest{Resource: crawlerIndex(config.Namespace, true) + "/s:" + mutation.ID, Action: weir.MutationAtomicTransform, BackendExpression: expression} return request, nil } diff --git a/internal/benchmark/partial_update_test.go b/internal/benchmark/partial_update_test.go index 84217da..c2e1c4c 100644 --- a/internal/benchmark/partial_update_test.go +++ b/internal/benchmark/partial_update_test.go @@ -52,7 +52,7 @@ func TestPartialUpdateNativeAndWeirUseIdenticalSingleDocWithoutLua(t *testing.T) t.Fatal(requests, err) } request := requests[0] - if request.Lua != nil || request.BackendExpression == nil || request.BackendExpression.ContentType != "application/vnd.weir.search-update.v1+json" || request.Action != weir.MutationAtomicTransform || request.Options.Visibility != weir.MutationQueryVisible { + if request.Lua != nil || request.BackendExpression == nil || request.BackendExpression.ContentType != "application/vnd.weir.search-update.v1+json" || request.Action != weir.MutationAtomicTransform || !crawlerQueryVisible(config) { t.Fatal("mechanism included Lua or changed completion semantics", request) } var payload map[string]any diff --git a/internal/soak/soak.go b/internal/soak/soak.go index 7d0f41f..ae8093f 100644 --- a/internal/soak/soak.go +++ b/internal/soak/soak.go @@ -158,7 +158,7 @@ func Run(ctx context.Context, config Config) (resultErr error) { backend: run.config.Kind, storeName: run.config.Store, store: run.store, - client: client, + client: sdkRecords{client: client}, keys: run.keys[index*run.config.KeysPerWorker : (index+1)*run.config.KeysPerWorker], padding: padding, profile: profileOf(config.Profiles, index, run.config.Workers), diff --git a/internal/soak/soak_test.go b/internal/soak/soak_test.go index cf63c3f..54c9a40 100644 --- a/internal/soak/soak_test.go +++ b/internal/soak/soak_test.go @@ -8,6 +8,7 @@ import ( "math/rand/v2" "net/http" "net/http/httptest" + "slices" "strconv" "strings" "testing" @@ -70,57 +71,66 @@ type step struct { } type scriptedClient struct { - store *memoryStore - steps []step - read *weir.ReadResult - native []byte - // mutate answers a Mutate call; mutateStream and readStream run a stream. - mutate func(weir.MutateOptions) ([]*weir.MutationResult, error) - reads func(weir.ReadOptions) ([]*weir.ReadResult, error) - mutateStream func(context.Context, weir.MutateStreamOptions) error - readStream func(context.Context, weir.ReadStreamOptions) error - transforms []*weir.AtomicTransformRequest -} - -func (c *scriptedClient) Put(_ context.Context, options weir.WriteOptions) (*weir.MutationResult, error) { + store *memoryStore + steps []step + readResult *weir.ReadResult + nativeBody []byte + // mutations answers a call of several records; streamMutations and streamReads + // run a stream. + mutations func([]*weir.MutateRequest) ([]*weir.MutationResult, error) + reads func([]*weir.ReadRequest) ([]*weir.ReadResult, error) + streamMutations func(context.Context, nextMutation, consumeMutation) error + streamReads func(context.Context, nextRead, consumeRead) error + transforms []*weir.MutateRequest + visible []bool +} + +// mutate answers a transform and a single Put from the script, and any other call +// through mutations. +func (c *scriptedClient) mutate(_ context.Context, _ string, visible bool, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { + c.visible = append(c.visible, visible) + if len(requests) != 1 { + return c.mutations(requests) + } + request := requests[0] + if request.Action == weir.MutationAtomicTransform { + c.transforms = append(c.transforms, request) + result := &weir.MutationResult{Outcome: weir.MutationApplied} + results := []*weir.MutationResult{result} + return results, nil + } + if c.mutations != nil { + return c.mutations(requests) + } next := c.steps[0] c.steps = c.steps[1:] if next.lands { - seq, _ := c.store.decode(options.Request.Document.Data) + seq, _ := c.store.decode(request.Document.Data) c.store.seqs["k"] = seq } result := &weir.MutationResult{Outcome: next.outcome} - return result, nil + results := []*weir.MutationResult{result} + return results, nil } -func (c *scriptedClient) ReadOne(context.Context, weir.ReadOneOptions) (*weir.ReadResult, error) { - return c.read, nil -} - -func (c *scriptedClient) AtomicTransform(_ context.Context, options weir.AtomicTransformOptions) (*weir.MutationResult, error) { - c.transforms = append(c.transforms, options.Request) - result := &weir.MutationResult{Outcome: weir.MutationApplied} - return result, nil -} - -func (c *scriptedClient) Mutate(_ context.Context, options weir.MutateOptions) ([]*weir.MutationResult, error) { - return c.mutate(options) -} - -func (c *scriptedClient) Read(_ context.Context, options weir.ReadOptions) ([]*weir.ReadResult, error) { - return c.reads(options) +func (c *scriptedClient) read(_ context.Context, _ string, requests ...*weir.ReadRequest) ([]*weir.ReadResult, error) { + if c.reads != nil { + return c.reads(requests) + } + results := []*weir.ReadResult{c.readResult} + return results, nil } -func (c *scriptedClient) MutateStream(ctx context.Context, options weir.MutateStreamOptions) error { - return c.mutateStream(ctx, options) +func (c *scriptedClient) mutateStream(ctx context.Context, _ string, next nextMutation, consume consumeMutation) error { + return c.streamMutations(ctx, next, consume) } -func (c *scriptedClient) ReadStream(ctx context.Context, options weir.ReadStreamOptions) error { - return c.readStream(ctx, options) +func (c *scriptedClient) readStream(ctx context.Context, _ string, next nextRead, consume consumeRead) error { + return c.streamReads(ctx, next, consume) } -func (c *scriptedClient) Native(ctx context.Context, options weir.NativeOptions) (*weir.NativeResult, error) { - if err := options.Consume(ctx, nil, c.native); err != nil { +func (c *scriptedClient) native(ctx context.Context, _ string, _ *weir.NativeRequest, chunks func(context.Context, *weir.NativeResponse, []byte) error) (*weir.NativeResult, error) { + if err := chunks(ctx, nil, c.nativeBody); err != nil { return nil, err } result := &weir.NativeResult{Completion: weir.NativeResponseComplete} @@ -193,12 +203,24 @@ func TestKeyAllowsOnlyTheLatestAppliedOrAnUnknownSequence(t *testing.T) { } } +func TestOnlyTheVisibleProfileWaitsForQueryVisibility(t *testing.T) { + store := &memoryStore{seqs: map[string]int64{}} + steps := []step{{outcome: weir.MutationApplied, lands: true}, {outcome: weir.MutationApplied, lands: true}} + client := &scriptedClient{store: store, steps: steps} + load, _ := newTestWorker(store, client) + load.putTurn(readyTokens(1)) + load.visibleTurn(readyTokens(1)) + if !slices.Equal(client.visible, []bool{false, true}) { + t.Fatalf("visible=%v", client.visible) + } +} + func TestRefusedWritesRetryWithFreshSequencesUntilApplied(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} steps := []step{{outcome: weir.MutationNotStarted}, {outcome: weir.MutationNotApplied}, {outcome: weir.MutationApplied, lands: true}} client := &scriptedClient{store: store, steps: steps} load, key := newTestWorker(store, client) - load.write(apiPut, key, load.putCall(nil)) + load.write(apiPut, key, load.putCall(false)) totals := load.stats.total() if key.applied != 3 || store.seqs["k"] != 3 || totals.Writes != 3 || totals.Retries != 2 || totals.NotStarted != 1 || totals.NotApplied != 1 || totals.Applied != 1 || totals.Verified != 3 { t.Fatalf("key=%+v store=%v totals=%+v", key, store.seqs, totals) @@ -213,7 +235,7 @@ func TestRefusalThatTookEffectIsAViolation(t *testing.T) { steps := []step{{outcome: weir.MutationNotApplied, lands: true}, {outcome: weir.MutationApplied, lands: true}} client := &scriptedClient{store: store, steps: steps} load, key := newTestWorker(store, client) - load.write(apiPut, key, load.putCall(nil)) + load.write(apiPut, key, load.putCall(false)) count, kept := load.violations.snapshot() if count != 1 || kept[0].Kind != "direct-after-refused" || kept[0].Observed != 1 || key.applied != 2 { t.Fatalf("count=%d kept=%+v key=%+v", count, kept, key) @@ -225,8 +247,8 @@ func TestUnknownWritesWidenWhatTheDatabaseMayHold(t *testing.T) { steps := []step{{outcome: weir.MutationUnknown, lands: true}, {outcome: weir.MutationUnknown}} client := &scriptedClient{store: store, steps: steps} load, key := newTestWorker(store, client) - load.write(apiPut, key, load.putCall(nil)) - load.write(apiPut, key, load.putCall(nil)) + load.write(apiPut, key, load.putCall(false)) + load.write(apiPut, key, load.putCall(false)) run := &backendRun{config: Backend{Kind: "test"}, store: store, keys: load.keys} check := finalCheck(run, load.violations) if len(key.unknown) != 2 || check.Violations != 0 || check.Present != 1 || check.Unknown != 1 { @@ -240,8 +262,8 @@ func TestLostAppliedWriteFailsTheFinalCheck(t *testing.T) { client := &scriptedClient{store: store, steps: steps} load, key := newTestWorker(store, client) load.verifyPercent = 0 - load.write(apiPut, key, load.putCall(nil)) - load.write(apiPut, key, load.putCall(nil)) + load.write(apiPut, key, load.putCall(false)) + load.write(apiPut, key, load.putCall(false)) run := &backendRun{config: Backend{Kind: "test"}, store: store, keys: load.keys} check := finalCheck(run, load.violations) if check.Violations != 1 || key.applied != 2 { @@ -278,7 +300,7 @@ func TestStopAbandonsARetryingWrite(t *testing.T) { stop := make(chan struct{}) close(stop) load.stop = stop - load.write(apiPut, key, load.putCall(nil)) + load.write(apiPut, key, load.putCall(false)) totals := load.stats.total() if totals.Abandoned != 1 || totals.VerifyUnavailable != 1 || key.applied != 0 { t.Fatalf("totals=%+v key=%+v", totals, key) @@ -288,7 +310,7 @@ func TestStopAbandonsARetryingWrite(t *testing.T) { func TestWeirReadsMustReturnAnAllowedSequence(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} document := &weir.Document{ContentType: "text/plain", Data: []byte("1")} - client := &scriptedClient{store: store, read: &weir.ReadResult{Document: document}} + client := &scriptedClient{store: store, readResult: &weir.ReadResult{Document: document}} load, key := newTestWorker(store, client) key.applied = 2 load.read(key) @@ -318,9 +340,9 @@ func TestLuaWritesStoreTheirInput(t *testing.T) { func TestMutateLeavesPositionsWithoutAResultUnknown(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} - client.mutate = func(options weir.MutateOptions) ([]*weir.MutationResult, error) { - if len(options.Requests) != 3 { - t.Errorf("requests=%d", len(options.Requests)) + client.mutations = func(requests []*weir.MutateRequest) ([]*weir.MutationResult, error) { + if len(requests) != 3 { + t.Errorf("requests=%d", len(requests)) } return []*weir.MutationResult{applied(), nil, notStarted()}, errors.New("broken") } @@ -360,12 +382,12 @@ func sendAll(t *testing.T, ctx context.Context, next func(context.Context) (*wei func TestMutateStreamLeavesRecordsWithoutAResultUnknown(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} - client.mutateStream = func(ctx context.Context, options weir.MutateStreamOptions) error { - sendAll(t, ctx, options.Next, 3) - if err := options.Consume(ctx, 1, applied()); err != nil { + client.streamMutations = func(ctx context.Context, next nextMutation, consume consumeMutation) error { + sendAll(t, ctx, next, 3) + if err := consume(ctx, 0, applied()); err != nil { t.Fatal(err) } - if err := options.Consume(ctx, 3, notStarted()); err != nil { + if err := consume(ctx, 2, notStarted()); err != nil { t.Fatal(err) } return status.Error(codes.Unavailable, "connection lost") @@ -388,21 +410,21 @@ func TestStreamChecksEveryRefusalBeforeTheKeyIsWrittenAgain(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} refused := &weir.MutationResult{Outcome: weir.MutationNotApplied} - client.mutateStream = func(ctx context.Context, options weir.MutateStreamOptions) error { + client.streamMutations = func(ctx context.Context, next nextMutation, consume consumeMutation) error { // The refused write took effect, and a later write replaces it. - if _, err := options.Next(ctx); err != nil { + if _, err := next(ctx); err != nil { t.Fatal(err) } store.seqs["k"] = 1 - if err := options.Consume(ctx, 1, refused); err != nil { + if err := consume(ctx, 0, refused); err != nil { t.Fatal(err) } // The only key stays busy until its check ran. - if _, err := options.Next(ctx); err != nil { + if _, err := next(ctx); err != nil { t.Fatal(err) } store.seqs["k"] = 2 - return options.Consume(ctx, 2, applied()) + return consume(ctx, 1, applied()) } load, key := newTestWorker(store, client) load.verifyPercent = 0 @@ -417,12 +439,12 @@ func TestStreamChecksEveryRefusalBeforeTheKeyIsWrittenAgain(t *testing.T) { func TestStreamSamplesAppliedWritesForTheDatabaseCheck(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} - client.mutateStream = func(ctx context.Context, options weir.MutateStreamOptions) error { - if _, err := options.Next(ctx); err != nil { + client.streamMutations = func(ctx context.Context, next nextMutation, consume consumeMutation) error { + if _, err := next(ctx); err != nil { t.Fatal(err) } // The database lost this APPLIED write. - return options.Consume(ctx, 1, applied()) + return consume(ctx, 0, applied()) } load, _ := newTestWorker(store, client) load.streamLifetime = time.Hour @@ -436,8 +458,8 @@ func TestStreamRecordsWithoutAResultMayLandAfterTheStreamGaveUp(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} var gaveUp time.Time - client.mutateStream = func(ctx context.Context, options weir.MutateStreamOptions) error { - if _, err := options.Next(ctx); err != nil { + client.streamMutations = func(ctx context.Context, next nextMutation, consume consumeMutation) error { + if _, err := next(ctx); err != nil { t.Fatal(err) } time.Sleep(10 * time.Millisecond) @@ -477,8 +499,8 @@ func TestStreamEndsItsInputAtItsLifetime(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} sent := 0 - client.mutateStream = func(ctx context.Context, options weir.MutateStreamOptions) error { - sent = sendAll(t, ctx, options.Next, 2) + client.streamMutations = func(ctx context.Context, next nextMutation, consume consumeMutation) error { + sent = sendAll(t, ctx, next, 2) return nil } load, _ := newTestWorker(store, client) @@ -543,14 +565,14 @@ func TestReadStreamChecksResultsAndCountsTheRestAsFailed(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} stale := &weir.Document{ContentType: "text/plain", Data: []byte("1")} client := &scriptedClient{store: store} - client.readStream = func(ctx context.Context, options weir.ReadStreamOptions) error { + client.streamReads = func(ctx context.Context, next nextRead, consume consumeRead) error { for range 2 { - if _, err := options.Next(ctx); err != nil { + if _, err := next(ctx); err != nil { t.Fatal(err) } } result := &weir.ReadResult{Document: stale} - if err := options.Consume(ctx, 1, result); err != nil { + if err := consume(ctx, 0, result); err != nil { t.Fatal(err) } return context.DeadlineExceeded @@ -573,7 +595,7 @@ func TestReadStreamChecksResultsAndCountsTheRestAsFailed(t *testing.T) { func TestReadBatchChecksEveryKey(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} client := &scriptedClient{store: store} - client.reads = func(weir.ReadOptions) ([]*weir.ReadResult, error) { + client.reads = func([]*weir.ReadRequest) ([]*weir.ReadResult, error) { missing := &weir.ReadResult{Missing: true} return []*weir.ReadResult{missing, nil}, errors.New("broken") } @@ -591,16 +613,16 @@ func TestReadBatchChecksEveryKey(t *testing.T) { func TestNativeReadsMustReturnAnAllowedSequence(t *testing.T) { store := &memoryStore{seqs: map[string]int64{}} - client := &scriptedClient{store: store, native: []byte("1")} + client := &scriptedClient{store: store, nativeBody: []byte("1")} load, key := newTestWorker(store, client) key.applied = 2 load.nativeTurn(readyTokens(1)) if count, kept := load.violations.snapshot(); count != 1 || kept[0].Kind != "weir-native" { t.Fatalf("count=%d kept=%+v", count, kept) } - client.native = []byte("refused") + client.nativeBody = []byte("refused") load.nativeTurn(readyTokens(1)) - client.native = []byte("2") + client.nativeBody = []byte("2") load.nativeTurn(readyTokens(1)) count, _ := load.violations.snapshot() api := load.stats.total().APIs[apiNative] diff --git a/internal/soak/worker.go b/internal/soak/worker.go index 70ea06a..16475e1 100644 --- a/internal/soak/worker.go +++ b/internal/soak/worker.go @@ -11,16 +11,54 @@ import ( weir "github.com/batchstream/weir-go" ) -// records is the part of the Weir SDK a worker calls. +// nextMutation and nextRead produce a stream's records, and consumeMutation and +// consumeRead receive its results, as WithNext and the consume options do. +type ( + nextMutation = func(context.Context) (*weir.MutateRequest, error) + consumeMutation = func(context.Context, int, *weir.MutationResult) error + nextRead = func(context.Context) (*weir.ReadRequest, error) + consumeRead = func(context.Context, int, *weir.ReadResult) error +) + +// records holds the SDK calls a worker makes. An SDK option cannot be inspected +// outside the SDK, so tests script this rather than the SDK's own methods. type records interface { - Put(ctx context.Context, options weir.WriteOptions) (*weir.MutationResult, error) - AtomicTransform(ctx context.Context, options weir.AtomicTransformOptions) (*weir.MutationResult, error) - Mutate(ctx context.Context, options weir.MutateOptions) ([]*weir.MutationResult, error) - MutateStream(ctx context.Context, options weir.MutateStreamOptions) error - ReadOne(ctx context.Context, options weir.ReadOneOptions) (*weir.ReadResult, error) - Read(ctx context.Context, options weir.ReadOptions) ([]*weir.ReadResult, error) - ReadStream(ctx context.Context, options weir.ReadStreamOptions) error - Native(ctx context.Context, options weir.NativeOptions) (*weir.NativeResult, error) + mutate(ctx context.Context, store string, visible bool, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) + mutateStream(ctx context.Context, store string, next nextMutation, consume consumeMutation) error + read(ctx context.Context, store string, requests ...*weir.ReadRequest) ([]*weir.ReadResult, error) + readStream(ctx context.Context, store string, next nextRead, consume consumeRead) error + native(ctx context.Context, store string, request *weir.NativeRequest, chunks func(context.Context, *weir.NativeResponse, []byte) error) (*weir.NativeResult, error) +} + +// sdkRecords makes a worker's calls through a Client. +type sdkRecords struct { + client *weir.Client +} + +func (s sdkRecords) mutate(ctx context.Context, store string, visible bool, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { + options := []weir.MutateOption{weir.WithRequests(requests...)} + if visible { + options = append(options, weir.WithQueryVisible()) + } + return s.client.Mutate(ctx, store, options...) +} + +func (s sdkRecords) mutateStream(ctx context.Context, store string, next nextMutation, consume consumeMutation) error { + _, err := s.client.Mutate(ctx, store, weir.WithNext(next), weir.WithMutateConsume(consume)) + return err +} + +func (s sdkRecords) read(ctx context.Context, store string, requests ...*weir.ReadRequest) ([]*weir.ReadResult, error) { + return s.client.Read(ctx, store, weir.WithRequests(requests...)) +} + +func (s sdkRecords) readStream(ctx context.Context, store string, next nextRead, consume consumeRead) error { + _, err := s.client.Read(ctx, store, weir.WithNext(next), weir.WithReadConsume(consume)) + return err +} + +func (s sdkRecords) native(ctx context.Context, store string, request *weir.NativeRequest, chunks func(context.Context, *weir.NativeResponse, []byte) error) (*weir.NativeResult, error) { + return s.client.Native(ctx, store, request, weir.WithChunks(chunks)) } // worker writes and reads its own keys through the SDK calls of its profile, @@ -197,11 +235,11 @@ func (w *worker) write(api string, key *keyState, call writeCall) { } } -func (w *worker) putCall(options *weir.MutationOptions) writeCall { +func (w *worker) putCall(visible bool) writeCall { call := func(ctx context.Context, key *keyState, document *weir.Document) (*weir.MutationResult, error) { - request := &weir.WriteRequest{Resource: w.store.resource(key.id), Document: document, Options: options} - writeOptions := weir.WriteOptions{StoreName: w.storeName, Request: request} - return w.client.Put(ctx, writeOptions) + request := &weir.MutateRequest{Resource: w.store.resource(key.id), Action: weir.MutationPut, Document: document} + results, err := w.client.mutate(ctx, w.storeName, visible, request) + return results[0], err } return call } @@ -212,16 +250,16 @@ const luaReplace = `return function(current, incoming) return incoming end` func (w *worker) luaCall(ctx context.Context, key *keyState, document *weir.Document) (*weir.MutationResult, error) { program := &weir.LuaTransform{Source: []byte(luaReplace), Input: document} - request := &weir.AtomicTransformRequest{Resource: w.store.resource(key.id), Lua: program} - options := weir.AtomicTransformOptions{StoreName: w.storeName, Request: request} - return w.client.AtomicTransform(ctx, options) + request := &weir.MutateRequest{Resource: w.store.resource(key.id), Action: weir.MutationAtomicTransform, Lua: program} + results, err := w.client.mutate(ctx, w.storeName, false, request) + return results[0], err } func (w *worker) putTurn(tokens <-chan struct{}) bool { if !w.take(tokens, 1) { return false } - w.write(apiPut, w.nextKey(), w.putCall(nil)) + w.write(apiPut, w.nextKey(), w.putCall(false)) return true } @@ -229,8 +267,7 @@ func (w *worker) visibleTurn(tokens <-chan struct{}) bool { if !w.take(tokens, 1) { return false } - visible := &weir.MutationOptions{Visibility: weir.MutationQueryVisible} - w.write(apiPutVisible, w.nextKey(), w.putCall(visible)) + w.write(apiPutVisible, w.nextKey(), w.putCall(true)) return true } @@ -263,8 +300,7 @@ func (w *worker) mutateTurn(tokens <-chan struct{}) bool { ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() started := time.Now() - options := weir.MutateOptions{StoreName: w.storeName, Requests: requests} - results, err := w.client.Mutate(ctx, options) + results, err := w.client.mutate(ctx, w.storeName, false, requests...) elapsed := time.Since(started) w.stats.called(apiMutate) if err != nil { @@ -296,7 +332,7 @@ type streamTurn struct { mu sync.Mutex started bool busy map[*keyState]bool - sent uint64 + sent int } type streamCheck struct { @@ -319,8 +355,9 @@ func (w *worker) newStreamTurn(api string, tokens <-chan struct{}, lifetime cont // reserve waits for room and a token for the next record, the first one // without waiting because the worker took its token before opening the stream, -// and returns the key it goes to. io.EOF ends the stream's input. -func (t *streamTurn) reserve(ctx context.Context) (*keyState, uint64, error) { +// and returns the key it goes to and the record's index. io.EOF ends the stream's +// input. +func (t *streamTurn) reserve(ctx context.Context) (*keyState, int, error) { t.mu.Lock() first := !t.started t.started = true @@ -348,8 +385,9 @@ func (t *streamTurn) reserve(ctx context.Context) (*keyState, uint64, error) { key = t.worker.nextKey() } t.busy[key] = true + index := t.sent t.sent++ - return key, t.sent, nil + return key, index, nil } // release returns a record's room once its result arrived. @@ -414,9 +452,8 @@ func (w *worker) mutateStreamTurn(tokens <-chan struct{}) bool { defer ended() turn := w.newStreamTurn(apiMutateStream, tokens, lifetime) turn.startChecks() - flight := make(map[uint64]pendingWrite) - options := weir.MutateStreamOptions{StoreName: w.storeName} - options.Next = func(ctx context.Context) (*weir.MutateRequest, error) { + flight := make(map[int]pendingWrite) + next := func(ctx context.Context) (*weir.MutateRequest, error) { key, index, err := turn.reserve(ctx) if err != nil { return nil, err @@ -431,7 +468,7 @@ func (w *worker) mutateStreamTurn(tokens <-chan struct{}) bool { request := &weir.MutateRequest{Resource: w.store.resource(key.id), Action: weir.MutationPut, Document: document} return request, nil } - options.Consume = func(_ context.Context, index uint64, result *weir.MutationResult) error { + consume := func(_ context.Context, index int, result *weir.MutationResult) error { turn.mu.Lock() defer turn.mu.Unlock() write := flight[index] @@ -439,7 +476,7 @@ func (w *worker) mutateStreamTurn(tokens <-chan struct{}) bool { turn.settled(write.key, w.settleStreamWrite(write, result)) return nil } - err := w.client.MutateStream(ctx, options) + err := w.client.mutateStream(ctx, w.storeName, next, consume) w.stats.called(apiMutateStream) if err != nil { w.stats.sdkError(apiMutateStream, err) @@ -466,17 +503,16 @@ func (w *worker) readOneTurn(tokens <-chan struct{}) bool { // reported outcomes allow. func (w *worker) read(key *keyState) { request := &weir.ReadRequest{Resource: w.store.resource(key.id)} - options := weir.ReadOneOptions{StoreName: w.storeName, Request: request} ctx, cancel := context.WithTimeout(context.Background(), w.timeout) started := time.Now() - result, err := w.client.ReadOne(ctx, options) + results, err := w.client.read(ctx, w.storeName, request) elapsed := time.Since(started) cancel() w.stats.called(apiReadOne) - if result == nil && err != nil { + if results[0] == nil && err != nil { w.stats.sdkError(apiReadOne, err) } - w.checkRead(apiReadOne, key, result, elapsed) + w.checkRead(apiReadOne, key, results[0], elapsed) } // checkRead records one read's result, which must return a sequence the @@ -516,8 +552,7 @@ func (w *worker) readBatchTurn(tokens <-chan struct{}) bool { ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() started := time.Now() - options := weir.ReadOptions{StoreName: w.storeName, Requests: requests} - results, err := w.client.Read(ctx, options) + results, err := w.client.read(ctx, w.storeName, requests...) elapsed := time.Since(started) w.stats.called(apiRead) if err != nil { @@ -546,9 +581,8 @@ func (w *worker) readStreamTurn(tokens <-chan struct{}) bool { key *keyState issued time.Time } - flight := make(map[uint64]pendingRead) - options := weir.ReadStreamOptions{StoreName: w.storeName} - options.Next = func(ctx context.Context) (*weir.ReadRequest, error) { + flight := make(map[int]pendingRead) + next := func(ctx context.Context) (*weir.ReadRequest, error) { key, index, err := turn.reserve(ctx) if err != nil { return nil, err @@ -560,7 +594,7 @@ func (w *worker) readStreamTurn(tokens <-chan struct{}) bool { request := &weir.ReadRequest{Resource: w.store.resource(key.id)} return request, nil } - options.Consume = func(_ context.Context, index uint64, result *weir.ReadResult) error { + consume := func(_ context.Context, index int, result *weir.ReadResult) error { turn.mu.Lock() defer turn.mu.Unlock() read := flight[index] @@ -569,7 +603,7 @@ func (w *worker) readStreamTurn(tokens <-chan struct{}) bool { turn.release(read.key) return nil } - err := w.client.ReadStream(ctx, options) + err := w.client.readStream(ctx, w.storeName, next, consume) w.stats.called(apiReadStream) if err != nil { w.stats.sdkError(apiReadStream, err) @@ -595,15 +629,14 @@ func (w *worker) nativeTurn(tokens <-chan struct{}) bool { return true } var body []byte - options := weir.NativeOptions{StoreName: w.storeName, Request: request} - options.Consume = func(_ context.Context, _ *weir.NativeResponse, chunk []byte) error { + chunks := func(_ context.Context, _ *weir.NativeResponse, chunk []byte) error { body = append(body, chunk...) return nil } ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() started := time.Now() - result, err := w.client.Native(ctx, options) + result, err := w.client.native(ctx, w.storeName, request, chunks) elapsed := time.Since(started) w.stats.called(apiNative) if err != nil { diff --git a/internal/workload/paths.go b/internal/workload/paths.go index 1d25ae4..83108e0 100644 --- a/internal/workload/paths.go +++ b/internal/workload/paths.go @@ -484,21 +484,18 @@ func (p *weirPath) Execute(ctx context.Context, operation Operation) Outcome { if operation.Write { if p.dataset.Config.LuaMutations { program := &weir.LuaTransform{Source: []byte(p.dataset.luaSource())} - request := &weir.AtomicTransformRequest{Resource: p.dataset.Resource(operation.Record), Lua: program} - opts := weir.AtomicTransformOptions{StoreName: p.dataset.Config.StoreName, Request: request} - result, err := p.client.AtomicTransform(ctx, opts) - return mutationOutcome(result, 0, err) + request := &weir.MutateRequest{Resource: p.dataset.Resource(operation.Record), Action: weir.MutationAtomicTransform, Lua: program} + results, err := p.client.Mutate(ctx, p.dataset.Config.StoreName, weir.WithRequests(request)) + return mutationOutcome(results[0], 0, err) } document := &weir.Document{ContentType: p.dataset.ContentType(), Data: p.dataset.Document(operation)} - request := &weir.WriteRequest{Resource: p.dataset.Resource(operation.Record), Document: document} - opts := weir.WriteOptions{StoreName: p.dataset.Config.StoreName, Request: request} - result, err := p.client.Put(ctx, opts) - return mutationOutcome(result, len(document.Data), err) + request := &weir.MutateRequest{Resource: p.dataset.Resource(operation.Record), Action: weir.MutationPut, Document: document} + results, err := p.client.Mutate(ctx, p.dataset.Config.StoreName, weir.WithRequests(request)) + return mutationOutcome(results[0], len(document.Data), err) } request := &weir.ReadRequest{Resource: p.dataset.Resource(operation.Record)} - opts := weir.ReadOneOptions{StoreName: p.dataset.Config.StoreName, Request: request} - result, err := p.client.ReadOne(ctx, opts) - return p.readOutcome(result, operation, err) + results, err := p.client.Read(ctx, p.dataset.Config.StoreName, weir.WithRequests(request)) + return p.readOutcome(results[0], operation, err) } func (p *weirPath) readOutcome(result *weir.ReadResult, operation Operation, rpcErr error) Outcome { diff --git a/internal/workload/weir_batch.go b/internal/workload/weir_batch.go index 928ff83..dd51dcc 100644 --- a/internal/workload/weir_batch.go +++ b/internal/workload/weir_batch.go @@ -31,8 +31,7 @@ func (p *weirPath) ExecuteBatch(ctx context.Context, operations []Operation) []O request := &weir.MutateRequest{Resource: p.dataset.Resource(operation.Record), Action: weir.MutationPut, Document: document} requests[index] = request } - opts := weir.MutateOptions{StoreName: p.dataset.Config.StoreName, Requests: requests} - replies, err := p.client.Mutate(ctx, opts) + replies, err := p.client.Mutate(ctx, p.dataset.Config.StoreName, weir.WithRequests(requests...)) if len(replies) != len(operations) { return batchFailure(operations, errors.Join(errors.New("Weir Mutate result count mismatch"), err)) } @@ -47,8 +46,7 @@ func (p *weirPath) ExecuteBatch(ctx context.Context, operations []Operation) []O request := &weir.ReadRequest{Resource: p.dataset.Resource(operation.Record)} requests[index] = request } - opts := weir.ReadOptions{StoreName: p.dataset.Config.StoreName, Requests: requests} - replies, err := p.client.Read(ctx, opts) + replies, err := p.client.Read(ctx, p.dataset.Config.StoreName, weir.WithRequests(requests...)) if len(replies) != len(operations) { return batchFailure(operations, errors.Join(errors.New("Weir Read result count mismatch"), err)) } diff --git a/internal/workload/weir_batch_test.go b/internal/workload/weir_batch_test.go index 83033c6..0578955 100644 --- a/internal/workload/weir_batch_test.go +++ b/internal/workload/weir_batch_test.go @@ -13,7 +13,6 @@ import ( pb "github.com/batchstream/weir-protocol/api/weir/v1" "google.golang.org/grpc" "google.golang.org/grpc/codes" - "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/status" "google.golang.org/grpc/test/bufconn" ) @@ -97,11 +96,10 @@ func TestSDKStreamReadPreservesConfirmedPrefixThroughLaterFailure(t *testing.T) first := Operation{Record: 0} second := Operation{Record: 1} peer := &acknowledgementPeer{document: dataset.Document(first)} - client := mutationTransport(t, peer) + client := mutationClient(t, peer) firstRequest := &weir.ReadRequest{Resource: dataset.Resource(first.Record)} secondRequest := &weir.ReadRequest{Resource: dataset.Resource(second.Record)} - options := weir.ReadOptions{StoreName: "search", Requests: []*weir.ReadRequest{firstRequest, secondRequest}} - replies, err := weir.Read(t.Context(), client, options) + replies, err := client.Read(t.Context(), "search", weir.WithRequests(firstRequest, secondRequest)) if err == nil || len(replies) != 2 || replies[0] == nil || replies[1] != nil || peer.calls.Load() != 1 { t.Fatal("unexpected partial read evidence", replies, err) } @@ -113,7 +111,8 @@ func TestSDKStreamReadPreservesConfirmedPrefixThroughLaterFailure(t *testing.T) } } -func mutationTransport(t *testing.T, peer *acknowledgementPeer) pb.StoreServiceClient { +// mutationClient returns a Client whose only server is peer, in memory. +func mutationClient(t *testing.T, peer *acknowledgementPeer) *weir.Client { t.Helper() listener := bufconn.Listen(1 << 20) server := grpc.NewServer() @@ -121,17 +120,14 @@ func mutationTransport(t *testing.T, peer *acknowledgementPeer) pb.StoreServiceC done := make(chan error, 1) go func() { done <- server.Serve(listener) }() t.Cleanup(func() { server.Stop(); listener.Close(); <-done }) - dialOptions := []grpc.DialOption{ - grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithNoProxy(), - grpc.WithDisableRetry(), grpc.WithDisableServiceConfig(), - grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) }), - } - connection, err := grpc.NewClient("passthrough:///offline-weir", dialOptions...) + dial := func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) } + // The address only labels the server the dialer reaches. + client, err := weir.New("127.0.0.1:1", weir.WithDialOptions(grpc.WithContextDialer(dial))) if err != nil { t.Fatal(err) } - t.Cleanup(func() { connection.Close() }) - return pb.NewStoreServiceClient(connection) + t.Cleanup(func() { client.Close() }) + return client } func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { @@ -141,11 +137,10 @@ func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { if mode == "canceled_after_apply" { peer.firstMutation = make(chan struct{}) } - client := mutationTransport(t, peer) + client := mutationClient(t, peer) document := &weir.Document{ContentType: "application/json", Data: []byte(`{"n":1}`)} first := &weir.MutateRequest{Resource: "records/s:first", Action: weir.MutationPut, Document: document} second := &weir.MutateRequest{Resource: "records/s:second", Action: weir.MutationPut, Document: document} - opts := weir.MutateOptions{StoreName: "search", Requests: []*weir.MutateRequest{first, second}} ctx, cancel := context.WithTimeout(t.Context(), time.Second) defer cancel() if mode == "canceled_after_apply" { @@ -157,7 +152,7 @@ func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { } }() } - replies, rpcErr := weir.Mutate(ctx, client, opts) + replies, rpcErr := client.Mutate(ctx, "search", weir.WithRequests(first, second)) applied := int64(1) if mode == "applied_failure" { applied = 2 @@ -188,13 +183,12 @@ func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { func TestSDKStreamMutationPreflightSendsNoRecord(t *testing.T) { peer := &acknowledgementPeer{mode: "applied_failure"} - client := mutationTransport(t, peer) + client := mutationClient(t, peer) document := &weir.Document{ContentType: "application/json", Data: []byte(`{"n":1}`)} first := &weir.MutateRequest{Resource: "records/s:first", Action: weir.MutationPut, Document: document} invalid := &weir.MutateRequest{Resource: "records/s:second", Action: 0, Document: document} - opts := weir.MutateOptions{StoreName: "search", Requests: []*weir.MutateRequest{first, invalid}} - replies, err := weir.Mutate(t.Context(), client, opts) - if err == nil || replies != nil || peer.calls.Load() != 0 || peer.applied.Load() != 0 { + replies, err := client.Mutate(t.Context(), "search", weir.WithRequests(first, invalid)) + if err == nil || len(replies) != 2 || replies[0] != nil || replies[1] != nil || peer.calls.Load() != 0 || peer.applied.Load() != 0 { t.Fatalf("late-invalid batch reached backend: replies=%v calls=%d err=%v", replies, peer.calls.Load(), err) } } diff --git a/tests/integration/backends_test.go b/tests/integration/backends_test.go index ff5a4c7..cebc333 100644 --- a/tests/integration/backends_test.go +++ b/tests/integration/backends_test.go @@ -101,18 +101,25 @@ func (b *backendData) resource(id string) string { return b.collection + "/s:" + weir.EncodeSegment(id) } -func (b *backendData) write(t *testing.T, id string, n int64) *weir.WriteRequest { +// write returns a Put of a record. +func (b *backendData) write(t *testing.T, id string, n int64) *weir.MutateRequest { + t.Helper() + return b.writeAs(t, weir.MutationPut, id, n) +} + +// writeAs returns a mutation that writes a record through action. +func (b *backendData) writeAs(t *testing.T, action weir.MutationAction, id string, n int64) *weir.MutateRequest { t.Helper() document := &weir.Document{ContentType: "application/json", Data: []byte(fmt.Sprintf("{\"n\":%d}", n))} if b.name == "mongo" { value := bson.D{{Key: "_id", Value: id}, {Key: "n", Value: n}} document = bsonDocument(t, value) } - request := &weir.WriteRequest{Resource: b.resource(id), Document: document} + request := &weir.MutateRequest{Resource: b.resource(id), Action: action, Document: document} return request } -func (b *backendData) transform(t *testing.T, id string) *weir.AtomicTransformRequest { +func (b *backendData) transform(t *testing.T, id string) *weir.MutateRequest { t.Helper() expression := &weir.Document{ ContentType: "application/vnd.weir.search-update.v1+json", @@ -124,7 +131,7 @@ func (b *backendData) transform(t *testing.T, id string) *weir.AtomicTransformRe expression = bsonDocument(t, update) expression.ContentType = "application/vnd.weir.mongodb-update.v1+bson" } - request := &weir.AtomicTransformRequest{Resource: b.resource(id), BackendExpression: expression} + request := &weir.MutateRequest{Resource: b.resource(id), Action: weir.MutationAtomicTransform, BackendExpression: expression} return request } @@ -243,9 +250,8 @@ func (b *backendData) httpDo(t *testing.T, ctx context.Context, request httpRequ func (b *backendData) assertRead(t *testing.T, ctx context.Context, client *weir.Client, want record) { t.Helper() request := &weir.ReadRequest{Resource: b.resource(want.id)} - options := weir.ReadOneOptions{StoreName: b.name, Request: request} rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - result, err := client.ReadOne(rpc, options) + result, err := readRecord(rpc, client, b.name, request) cancel() b.assertReadResult(t, result, err, want.n) } @@ -264,9 +270,8 @@ func (b *backendData) assertReadResult(t *testing.T, result *weir.ReadResult, er func (b *backendData) waitRead(t *testing.T, ctx context.Context, client *weir.Client, want record) { t.Helper() request := &weir.ReadRequest{Resource: b.resource(want.id)} - options := weir.ReadOneOptions{StoreName: b.name, Request: request} poll(t, ctx, "read-only recovery "+b.name, func(attempt context.Context) error { - result, err := client.ReadOne(attempt, options) + result, err := readRecord(attempt, client, b.name, request) if err != nil { return err } diff --git a/tests/integration/batch_test.go b/tests/integration/batch_test.go index 2e96ced..95454c1 100644 --- a/tests/integration/batch_test.go +++ b/tests/integration/batch_test.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "net/http" + "slices" "testing" weir "github.com/batchstream/weir-go" @@ -30,9 +31,8 @@ func testPublicBatch(t *testing.T, ctx context.Context, client *weir.Client, bac // without converting successful independent mutations into an RPC error. duplicate := backend.write(t, "primary", 999) mutations[24] = &weir.MutateRequest{Resource: duplicate.Resource, Action: weir.MutationCreate, Document: duplicate.Document} - opts := weir.MutateOptions{StoreName: backend.name, Requests: mutations} rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - results, err := client.Mutate(rpc, opts) + results, err := mutateRecords(rpc, client, backend.name, mutations...) cancel() if err != nil || len(results) != count { t.Fatalf("public batch Mutate: length=%d error=%v", len(results), err) @@ -55,9 +55,8 @@ func testPublicBatch(t *testing.T, ctx context.Context, client *weir.Client, bac } missing := &weir.ReadRequest{Resource: backend.resource("public_batch_absent")} reads[count] = missing - readOptions := weir.ReadOptions{StoreName: backend.name, Requests: reads} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - readResults, err := client.Read(rpc, readOptions) + readResults, err := readRecords(rpc, client, backend.name, reads...) cancel() if err != nil || len(readResults) != count+1 { t.Fatalf("public batch Read: length=%d error=%v", len(readResults), err) @@ -81,11 +80,11 @@ func testPublicBatch(t *testing.T, ctx context.Context, client *weir.Client, bac rejected[index] = request } rejected[count-1].Action = 0 - invalidOptions := weir.MutateOptions{StoreName: backend.name, Requests: rejected} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - invalidResults, err := client.Mutate(rpc, invalidOptions) + invalidResults, err := mutateRecords(rpc, client, backend.name, rejected...) cancel() - if err == nil || invalidResults != nil { + confirmed := func(result *weir.MutationResult) bool { return result != nil } + if err == nil || slices.ContainsFunc(invalidResults, confirmed) { t.Fatalf("invalid request slice accepted: length=%d error=%v", len(invalidResults), err) } for index := range count { @@ -140,9 +139,8 @@ func testDuplicateURIOrder(t *testing.T, ctx context.Context, client *weir.Clien } requests[index] = request } - opts := weir.MutateOptions{StoreName: backend.name, Requests: requests} rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - results, err := client.Mutate(rpc, opts) + results, err := mutateRecords(rpc, client, backend.name, requests...) cancel() if err != nil || len(results) != len(requests) { t.Fatalf("same-URI mutation batch: results=%v err=%v", results, err) @@ -170,9 +168,8 @@ func testDuplicateURIOrder(t *testing.T, ctx context.Context, client *weir.Clien reads[index] = primary } } - readOpts := weir.ReadOptions{StoreName: backend.name, Requests: reads} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - readResults, err := client.Read(rpc, readOpts) + readResults, err := readRecords(rpc, client, backend.name, reads...) cancel() if err != nil || len(readResults) != len(reads) { t.Fatalf("513 duplicate URI reads: results=%d err=%v", len(readResults), err) @@ -264,9 +261,8 @@ func (s *system) testLargeDistinctBatch(t *testing.T, backend *backendData) { } s.quiesce(t) before := s.nodeMetrics(t) - opts := weir.MutateOptions{StoreName: backend.name, Requests: mutations} rpc, cancel := context.WithTimeout(s.ctx, rpcTimeout) - results, err := s.client.Mutate(rpc, opts) + results, err := mutateRecords(rpc, s.client, backend.name, mutations...) cancel() if err != nil || len(results) != count { t.Fatalf("513 distinct Put batch: results=%d err=%v", len(results), err) @@ -274,15 +270,14 @@ func (s *system) testLargeDistinctBatch(t *testing.T, backend *backendData) { for _, result := range results { assertApplied(t, result, nil) } - evidence := streamBatchEvidence{Before: before, After: s.nodeMetrics(t), Store: backend.name, Method: "mutate", Records: count} + evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "mutate", Records: count} assertStreamBatchMetrics(t, evidence) for index := range mutations { backend.assertPersisted(t, s.ctx, fmt.Sprintf("large_distinct_%03d", index), int64(10000+index)) } before = s.nodeMetrics(t) - readOptions := weir.ReadOptions{StoreName: backend.name, Requests: reads} rpc, cancel = context.WithTimeout(s.ctx, rpcTimeout) - readResults, err := s.client.Read(rpc, readOptions) + readResults, err := readRecords(rpc, s.client, backend.name, reads...) cancel() if err != nil || len(readResults) != count { t.Fatalf("513 distinct Read batch: results=%d err=%v", len(readResults), err) @@ -290,7 +285,7 @@ func (s *system) testLargeDistinctBatch(t *testing.T, backend *backendData) { for index, result := range readResults { backend.assertReadResult(t, result, nil, int64(10000+count-1-index)) } - evidence.Before, evidence.After, evidence.Method = before, s.nodeMetrics(t), "read" + evidence.Before, evidence.After, evidence.Method = before, s.metricsAfterStream(t, before), "read" assertStreamBatchMetrics(t, evidence) t.Log("513 distinct native documents Put and Read in aggregation groups on one streamed call; every value independently persisted and reverse-order results verified") } diff --git a/tests/integration/benchmark_test.go b/tests/integration/benchmark_test.go index 4773591..247b382 100644 --- a/tests/integration/benchmark_test.go +++ b/tests/integration/benchmark_test.go @@ -1,4 +1,4 @@ -//go:build integration +//go:build integration && !previous_sdk package integration_test diff --git a/tests/integration/lifecycle_test.go b/tests/integration/lifecycle_test.go index d0185b7..18f80d3 100644 --- a/tests/integration/lifecycle_test.go +++ b/tests/integration/lifecycle_test.go @@ -12,44 +12,41 @@ import ( "testing" weir "github.com/batchstream/weir-go" - pb "github.com/batchstream/weir-protocol/api/weir/v1" "github.com/batchstream/weir-tests/internal/fixture" "go.mongodb.org/mongo-driver/v2/bson" ) func testLifecycle(t *testing.T, ctx context.Context, client *weir.Client, backend *backendData) { t.Helper() - write := backend.write(t, "primary", 1) - options := weir.WriteOptions{StoreName: backend.name, Request: write} + write := backend.writeAs(t, weir.MutationCreate, "primary", 1) rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - result, err := client.Create(rpc, options) + result, err := mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Create(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertPrecondition(t, result, err) - options.Request = backend.write(t, "missing_replace", 42) + write = backend.writeAs(t, weir.MutationReplace, "missing_replace", 42) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Replace(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertPrecondition(t, result, err) backend.assertMissing(t, ctx, "missing_replace") - options.Request = backend.write(t, "primary", 2) + write = backend.writeAs(t, weir.MutationReplace, "primary", 2) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Replace(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) - options.Request = backend.write(t, "primary", 3) + write = backend.write(t, "primary", 3) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Put(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) transform := backend.transform(t, "primary") - transformOptions := weir.AtomicTransformOptions{StoreName: backend.name, Request: transform} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.AtomicTransform(rpc, transformOptions) + result, err = mutateRecord(rpc, client, backend.name, transform) cancel() assertApplied(t, result, err) want := record{id: "primary", n: 4} @@ -58,30 +55,28 @@ func testLifecycle(t *testing.T, ctx context.Context, client *weir.Client, backe testTypedSequence(t, ctx, client, backend) testNative(t, ctx, client, backend) - options.Request = backend.write(t, "disposable", 7) + write = backend.write(t, "disposable", 7) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Put(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) - remove := &weir.DeleteRequest{Resource: backend.resource("disposable")} - removeOptions := weir.DeleteOptions{StoreName: backend.name, Request: remove} + remove := &weir.MutateRequest{Resource: backend.resource("disposable"), Action: weir.MutationDelete} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Delete(rpc, removeOptions) + result, err = mutateRecord(rpc, client, backend.name, remove) cancel() assertApplied(t, result, err) read := &weir.ReadRequest{Resource: backend.resource("disposable")} - readOptions := weir.ReadOneOptions{StoreName: backend.name, Request: read} rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - missing, err := client.ReadOne(rpc, readOptions) + missing, err := readRecord(rpc, client, backend.name, read) cancel() if err != nil || missing == nil || !missing.Missing || missing.Failure != nil { t.Fatalf("Read after Delete: result=%v err=%v", missing, err) } backend.assertMissing(t, ctx, "disposable") for i := 1; i <= 2; i++ { - options.Request = backend.write(t, fmt.Sprintf("page%d", i), int64(10+i)) + write = backend.writeAs(t, weir.MutationCreate, fmt.Sprintf("page%d", i), int64(10+i)) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - result, err = client.Create(rpc, options) + result, err = mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) } @@ -105,26 +100,23 @@ func assertPrecondition(t *testing.T, result *weir.MutationResult, err error) { func testTypedSequence(t *testing.T, ctx context.Context, client *weir.Client, backend *backendData) { t.Helper() read := &weir.ReadRequest{Resource: backend.resource("primary")} - readOptions := weir.ReadOptions{StoreName: backend.name, Requests: []*weir.ReadRequest{read}} rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - before, err := client.Read(rpc, readOptions) + before, err := readRecords(rpc, client, backend.name, read) cancel() if err != nil || len(before) != 1 { t.Fatalf("typed Read before mutation: results=%v err=%v", before, err) } backend.assertReadResult(t, before[0], nil, 4) - write := backend.write(t, "primary", 5) - mutation := &weir.MutateRequest{Resource: write.Resource, Action: weir.MutationReplace, Document: write.Document} - mutationOptions := weir.MutateOptions{StoreName: backend.name, Requests: []*weir.MutateRequest{mutation}} + mutation := backend.writeAs(t, weir.MutationReplace, "primary", 5) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - results, err := client.Mutate(rpc, mutationOptions) + results, err := mutateRecords(rpc, client, backend.name, mutation) cancel() if err != nil || len(results) != 1 { t.Fatalf("typed Mutate: results=%v err=%v", results, err) } assertApplied(t, results[0], nil) rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - after, err := client.Read(rpc, readOptions) + after, err := readRecords(rpc, client, backend.name, read) cancel() if err != nil || len(after) != 1 { t.Fatalf("typed Read after mutation: results=%v err=%v", after, err) @@ -142,14 +134,14 @@ func testCrossReplicaNativePagination(t *testing.T, ctx context.Context, cluster t.Fatalf("refresh owned Search index: status=%d body=%s", code, body) } } - transports := []pb.StoreServiceClient{ - transport(t, cluster.Nodes[0].Application), transport(t, cluster.Nodes[1].Application), + replicas := []*weir.Client{ + replicaClient(t, cluster.Nodes[0].Application), replicaClient(t, cluster.Nodes[1].Application), } calls := 0 - invoke := func(ctx context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { - wire := transports[calls%len(transports)] + invoke := func(ctx context.Context, call nativeCall) (*weir.NativeResult, error) { + replica := replicas[calls%len(replicas)] calls++ - return weir.Native(ctx, wire, opts) + return runNative(ctx, replica, call) } query := nativeQuery{} pager, err := backend.nativePager(invoke, query) @@ -227,8 +219,8 @@ func testNative(t *testing.T, ctx context.Context, client *weir.Client, backend } var head *weir.NativeResponse var body []byte - options := weir.NativeOptions{StoreName: backend.name, Request: request} - options.Consume = func(_ context.Context, response *weir.NativeResponse, chunk []byte) error { + call := nativeCall{store: backend.name, request: request} + call.consume = func(_ context.Context, response *weir.NativeResponse, chunk []byte) error { if response == nil || head != nil && head != response { return errors.New("Native response metadata changed between chunks") } @@ -240,15 +232,15 @@ func testNative(t *testing.T, ctx context.Context, client *weir.Client, backend return nil } rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - end, err := client.Native(rpc, options) + end, err := runNative(rpc, client, call) cancel() if err != nil || head == nil || end == nil || end.Response != head || end.Failure != nil || end.Completion != weir.NativeResponseComplete { t.Fatalf("Native evidence: head=%v end=%v err=%v", head, end, err) } refused := errors.New("Native consumer refused body") - options.Consume = func(context.Context, *weir.NativeResponse, []byte) error { return refused } + call.consume = func(context.Context, *weir.NativeResponse, []byte) error { return refused } rpc, cancel = context.WithTimeout(ctx, rpcTimeout) - interrupted, err := client.Native(rpc, options) + interrupted, err := runNative(rpc, client, call) cancel() if !errors.Is(err, refused) || interrupted == nil || interrupted.Response == nil || interrupted.Completion == weir.NativeResponseComplete { t.Fatalf("refused Native body claimed complete transfer: result=%v err=%v", interrupted, err) diff --git a/tests/integration/native_pagination_test.go b/tests/integration/native_pagination_test.go index f3f6180..b19279f 100644 --- a/tests/integration/native_pagination_test.go +++ b/tests/integration/native_pagination_test.go @@ -12,7 +12,6 @@ import ( "testing" weir "github.com/batchstream/weir-go" - pb "github.com/batchstream/weir-protocol/api/weir/v1" "go.mongodb.org/mongo-driver/v2/bson" ) @@ -24,14 +23,6 @@ const ( nativeMaxBytes = nativeMaxBody * nativeMaxCalls ) -type nativeInvoke func(context.Context, weir.NativeOptions) (*weir.NativeResult, error) - -func replicaNative(wire pb.StoreServiceClient) nativeInvoke { - return func(ctx context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { - return weir.Native(ctx, wire, opts) - } -} - type nativeHTTPQuery struct { resource, method, path string body any @@ -84,8 +75,8 @@ func (p *nativePager) exchange(ctx context.Context, request *weir.NativeRequest) p.calls++ var head *weir.NativeResponse var body []byte - opts := weir.NativeOptions{StoreName: p.store, Request: request} - opts.Consume = func(_ context.Context, response *weir.NativeResponse, chunk []byte) error { + call := nativeCall{store: p.store, request: request} + call.consume = func(_ context.Context, response *weir.NativeResponse, chunk []byte) error { if response == nil || head != nil && head != response { return errors.New("Native metadata changed") } @@ -99,7 +90,7 @@ func (p *nativePager) exchange(ctx context.Context, request *weir.NativeRequest) } rpc, cancel := context.WithTimeout(ctx, rpcTimeout) defer cancel() - end, err := p.invoke(rpc, opts) + end, err := p.invoke(rpc, call) if err != nil { return nil, nil, err } diff --git a/tests/integration/native_pagination_unit_test.go b/tests/integration/native_pagination_unit_test.go index 28c909d..ac49f17 100644 --- a/tests/integration/native_pagination_unit_test.go +++ b/tests/integration/native_pagination_unit_test.go @@ -21,14 +21,14 @@ import ( // These tests exercise only consumer parsing, budgets and cleanup. They are not // evidence of real MongoDB/Search behavior or Store routing. -func nativeTestReply(opts weir.NativeOptions, body []byte, status int) (*weir.NativeResult, error) { +func nativeTestReply(opts nativeCall, body []byte, status int) (*weir.NativeResult, error) { head := &weir.NativeResponse{} if status != 0 { metadata := &weir.Document{ContentType: "application/http", Data: fmt.Appendf(nil, "HTTP/1.1 %d %s\r\n\r\n", status, http.StatusText(status))} head.Metadata = metadata } if len(body) != 0 { - if err := opts.Consume(context.Background(), head, body); err != nil { + if err := opts.consume(context.Background(), head, body); err != nil { return nil, err } } @@ -76,9 +76,9 @@ func testNativeHTTPRequest(t *testing.T, request *weir.NativeRequest) (*http.Req func TestNativePaginationMongoPages(t *testing.T) { calls := 0 - invoke := func(_ context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { - command := bson.Raw(opts.Request.Request.Data) - if opts.StoreName != "mongo" || opts.Request.Resource != "owned" { + invoke := func(_ context.Context, opts nativeCall) (*weir.NativeResult, error) { + command := bson.Raw(opts.request.Request.Data) + if opts.store != "mongo" || opts.request.Resource != "owned" { t.Fatal("Mongo Native routing changed", opts) } batch := "next" @@ -121,18 +121,18 @@ func TestNativePaginationMongoPages(t *testing.T) { func TestNativePaginationSearchRotatingScrollAndCleanup(t *testing.T) { calls := 0 - invoke := func(ctx context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { - request, body := testNativeHTTPRequest(t, opts.Request) + invoke := func(ctx context.Context, opts nativeCall) (*weir.NativeResult, error) { + request, body := testNativeHTTPRequest(t, opts.request) var response string switch calls { case 0: - if opts.Request.Resource != "owned" || request.URL.RequestURI() != "/_search?scroll=1m" { + if opts.request.Resource != "owned" || request.URL.RequestURI() != "/_search?scroll=1m" { t.Fatal("wrong Search opening request") } response = `{"_scroll_id":"a","hits":{"hits":[{"_source":{"n":1}}]}}` case 1, 2: want := []string{"", "a", "b"}[calls] - if opts.Request.Resource != "" || request.Method != http.MethodPost || request.URL.Path != "/_search/scroll" || body["scroll_id"] != want { + if opts.request.Resource != "" || request.Method != http.MethodPost || request.URL.Path != "/_search/scroll" || body["scroll_id"] != want { t.Fatal("wrong backend-owned scroll continuation", body) } response = `{"_scroll_id":"b","hits":{"hits":[{"_source":{"n":2}}]}}` @@ -181,7 +181,7 @@ func TestNativePaginationCleanupAfterCanceledQuery(t *testing.T) { for _, store := range []string{"mongo", "search"} { t.Run(store, func(t *testing.T) { calls := 0 - invoke := func(ctx context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { + invoke := func(ctx context.Context, opts nativeCall) (*weir.NativeResult, error) { if calls == 0 { calls++ return nil, ctx.Err() @@ -196,7 +196,7 @@ func TestNativePaginationCleanupAfterCanceledQuery(t *testing.T) { if store == "search" { return nativeTestReply(opts, []byte(`{"succeeded":true,"num_freed":1}`), http.StatusOK) } - raw := bson.Raw(opts.Request.Request.Data) + raw := bson.Raw(opts.request.Request.Data) if raw.Lookup("killCursors").StringValue() != "records" { t.Fatal("wrong Mongo cleanup", raw) } @@ -223,7 +223,7 @@ func TestNativePaginationCleanupAfterCanceledQuery(t *testing.T) { func TestNativePaginationBounds(t *testing.T) { t.Run("pages_reserve_cleanup", func(t *testing.T) { calls := 0 - invoke := func(_ context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { + invoke := func(_ context.Context, opts nativeCall) (*weir.NativeResult, error) { calls++ body := `{"_scroll_id":"live","hits":{"hits":[{"_source":{"n":1}}]}}` if calls > nativeMaxPages { @@ -247,7 +247,7 @@ func TestNativePaginationBounds(t *testing.T) { for _, bound := range []string{"body", "total", "calls"} { t.Run(bound, func(t *testing.T) { calls := 0 - invoke := func(_ context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { + invoke := func(_ context.Context, opts nativeCall) (*weir.NativeResult, error) { calls++ body := []byte("xx") if bound == "body" { @@ -297,7 +297,7 @@ func TestNativePaginationMalformedPagesAndBusinessErrors(t *testing.T) { if _, err := pager.mongoPage(body, true); err == nil || pager.mongoCursor != 17 || pager.exhausted { t.Fatal("malformed Mongo page lost cleanup identity", err, pager) } - invoke := func(_ context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { + invoke := func(_ context.Context, opts nativeCall) (*weir.NativeResult, error) { return nativeTestReply(opts, []byte(`{"error":{"type":"backend_failure"}}`), http.StatusInternalServerError) } pager = &nativePager{store: "search", invoke: invoke} @@ -311,7 +311,7 @@ func TestNativePaginationMalformedPagesAndBusinessErrors(t *testing.T) { } func TestNativePaginationIncompleteTransferAndCleanupFailure(t *testing.T) { - invoke := func(_ context.Context, _ weir.NativeOptions) (*weir.NativeResult, error) { + invoke := func(_ context.Context, _ nativeCall) (*weir.NativeResult, error) { head := &weir.NativeResponse{} result := &weir.NativeResult{Response: head, Completion: weir.NativeResponseIncomplete} return result, nil @@ -321,7 +321,7 @@ func TestNativePaginationIncompleteTransferAndCleanupFailure(t *testing.T) { t.Fatal("partial transfer accepted") } pager.scrollID = "live" - pager.invoke = func(_ context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { + pager.invoke = func(_ context.Context, opts nativeCall) (*weir.NativeResult, error) { return nativeTestReply(opts, []byte(`{"succeeded":true,"num_freed":0}`), http.StatusOK) } if err := pager.close(); err == nil || pager.closed { diff --git a/tests/integration/sdk_current_test.go b/tests/integration/sdk_current_test.go new file mode 100644 index 0000000..ad8d8c0 --- /dev/null +++ b/tests/integration/sdk_current_test.go @@ -0,0 +1,36 @@ +//go:build integration && !previous_sdk + +package integration_test + +import ( + "context" + + weir "github.com/batchstream/weir-go" +) + +func readRecord(ctx context.Context, client *weir.Client, store string, request *weir.ReadRequest) (*weir.ReadResult, error) { + results, err := client.Read(ctx, store, weir.WithRequests(request)) + return results[0], err +} + +func readRecords(ctx context.Context, client *weir.Client, store string, requests ...*weir.ReadRequest) ([]*weir.ReadResult, error) { + return client.Read(ctx, store, weir.WithRequests(requests...)) +} + +func mutateRecord(ctx context.Context, client *weir.Client, store string, request *weir.MutateRequest) (*weir.MutationResult, error) { + results, err := client.Mutate(ctx, store, weir.WithRequests(request)) + return results[0], err +} + +func mutateRecords(ctx context.Context, client *weir.Client, store string, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { + return client.Mutate(ctx, store, weir.WithRequests(requests...)) +} + +func streamReads(ctx context.Context, client *weir.Client, stream readStream) error { + _, err := client.Read(ctx, stream.store, weir.WithNext(stream.next), weir.WithReadConsume(stream.consume)) + return err +} + +func runNative(ctx context.Context, client *weir.Client, call nativeCall) (*weir.NativeResult, error) { + return client.Native(ctx, call.store, call.request, weir.WithChunks(call.consume)) +} diff --git a/tests/integration/sdk_previous_test.go b/tests/integration/sdk_previous_test.go new file mode 100644 index 0000000..033117e --- /dev/null +++ b/tests/integration/sdk_previous_test.go @@ -0,0 +1,70 @@ +//go:build integration && previous_sdk + +package integration_test + +import ( + "context" + "fmt" + "testing" + + weir "github.com/batchstream/weir-go" + "github.com/batchstream/weir-tests/internal/fixture" +) + +func readRecord(ctx context.Context, client *weir.Client, store string, request *weir.ReadRequest) (*weir.ReadResult, error) { + options := weir.ReadOneOptions{StoreName: store, Request: request} + return client.ReadOne(ctx, options) +} + +func readRecords(ctx context.Context, client *weir.Client, store string, requests ...*weir.ReadRequest) ([]*weir.ReadResult, error) { + options := weir.ReadOptions{StoreName: store, Requests: requests} + return client.Read(ctx, options) +} + +// mutateRecord uses the single-record method of the action, which shares streams. +func mutateRecord(ctx context.Context, client *weir.Client, store string, request *weir.MutateRequest) (*weir.MutationResult, error) { + write := &weir.WriteRequest{Resource: request.Resource, Document: request.Document} + writeOptions := weir.WriteOptions{StoreName: store, Request: write} + switch request.Action { + case weir.MutationCreate: + return client.Create(ctx, writeOptions) + case weir.MutationPut: + return client.Put(ctx, writeOptions) + case weir.MutationReplace: + return client.Replace(ctx, writeOptions) + case weir.MutationDelete: + remove := &weir.DeleteRequest{Resource: request.Resource} + options := weir.DeleteOptions{StoreName: store, Request: remove} + return client.Delete(ctx, options) + case weir.MutationAtomicTransform: + transform := &weir.AtomicTransformRequest{Resource: request.Resource, Lua: request.Lua, BackendExpression: request.BackendExpression} + options := weir.AtomicTransformOptions{StoreName: store, Request: transform} + return client.AtomicTransform(ctx, options) + } + return nil, fmt.Errorf("mutation action %d has no single-record method", request.Action) +} + +func mutateRecords(ctx context.Context, client *weir.Client, store string, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { + options := weir.MutateOptions{StoreName: store, Requests: requests} + return client.Mutate(ctx, options) +} + +// streamReads turns the previous SDK's indexes, which start at 1, into positions. +func streamReads(ctx context.Context, client *weir.Client, stream readStream) error { + consume := func(ctx context.Context, index uint64, result *weir.ReadResult) error { + return stream.consume(ctx, int(index)-1, result) + } + options := weir.ReadStreamOptions{StoreName: stream.store, Next: stream.next, Consume: consume} + return client.ReadStream(ctx, options) +} + +func runNative(ctx context.Context, client *weir.Client, call nativeCall) (*weir.NativeResult, error) { + options := weir.NativeOptions{StoreName: call.store, Request: call.request, Consume: call.consume} + return client.Native(ctx, options) +} + +// testNativeWriteChangeEvidence lives with the benchmark workload, which the current +// SDK builds. It checks the databases, not an SDK. +func testNativeWriteChangeEvidence(t *testing.T, _ context.Context, _ *fixture.Cluster) { + t.Skip("the benchmark workload needs the current SDK") +} diff --git a/tests/integration/sdk_test.go b/tests/integration/sdk_test.go new file mode 100644 index 0000000..ca4841d --- /dev/null +++ b/tests/integration/sdk_test.go @@ -0,0 +1,54 @@ +//go:build integration + +package integration_test + +import ( + "context" + "testing" + + weir "github.com/batchstream/weir-go" +) + +// These tests run against the current SDK and, with the previous_sdk tag, the one +// before it. sdk_current_test.go and sdk_previous_test.go hold the calls whose API +// differs, with these types in common. + +// readStream is a read call fed record by record, with results handed over as they +// complete. The index is the record's position in the input, from 0. +type readStream struct { + store string + next func(context.Context) (*weir.ReadRequest, error) + consume func(ctx context.Context, index int, result *weir.ReadResult) error +} + +// nativeCall is one Native call whose body goes to consume. +type nativeCall struct { + store string + request *weir.NativeRequest + consume func(context.Context, *weir.NativeResponse, []byte) error +} + +type nativeInvoke func(context.Context, nativeCall) (*weir.NativeResult, error) + +// clientNative sends Native calls through client. +func clientNative(client *weir.Client) nativeInvoke { + invoke := func(ctx context.Context, call nativeCall) (*weir.NativeResult, error) { + return runNative(ctx, client, call) + } + return invoke +} + +// replicaClient returns a Client of one replica, whose address resolves to it alone. +func replicaClient(t *testing.T, address string) *weir.Client { + t.Helper() + client, err := weir.New(address) + if err != nil { + t.Fatal("create SDK client for a replica:", err) + } + t.Cleanup(func() { + if err := client.Close(); err != nil { + t.Error("close SDK client of a replica:", err) + } + }) + return client +} diff --git a/tests/integration/semantics_test.go b/tests/integration/semantics_test.go index afbad16..09ae499 100644 --- a/tests/integration/semantics_test.go +++ b/tests/integration/semantics_test.go @@ -38,17 +38,15 @@ func (s *system) testLuaSemantics(t *testing.T, backend *backendData) { t.Run(fmt.Sprintf("lua/%s/existing=%t", tc.name, existing), func(t *testing.T) { id := fmt.Sprintf("lua_%s_%t", tc.name, existing) if existing { - write := backend.write(t, id, 1) - opts := weir.WriteOptions{StoreName: backend.name, Request: write} - result, err := s.client.Create(s.ctx, opts) + write := backend.writeAs(t, weir.MutationCreate, id, 1) + result, err := mutateRecord(s.ctx, s.client, backend.name, write) assertApplied(t, result, err) } input := backend.write(t, id, 42).Document program := &weir.LuaTransform{Source: []byte(tc.source), Input: input} - request := &weir.AtomicTransformRequest{Resource: backend.resource(id), Lua: program} - opts := weir.AtomicTransformOptions{StoreName: backend.name, Request: request} + request := &weir.MutateRequest{Resource: backend.resource(id), Action: weir.MutationAtomicTransform, Lua: program} rpc, cancel := context.WithTimeout(s.ctx, rpcTimeout) - result, err := s.client.AtomicTransform(rpc, opts) + result, err := mutateRecord(rpc, s.client, backend.name, request) cancel() failure := tc.failure if tc.name == "current" && !existing { @@ -88,16 +86,15 @@ end`) start := make(chan struct{}) finished := make(chan error, 2) for _, node := range s.cluster.Nodes[:2] { - wire := transport(t, node.Application) + replica := replicaClient(t, node.Application) input := backend.write(t, id, 1).Document go func() { <-start for range iterations { program := &weir.LuaTransform{Source: source, Input: input} - request := &weir.AtomicTransformRequest{Resource: backend.resource(id), Lua: program} - opts := weir.AtomicTransformOptions{StoreName: backend.name, Request: request} + request := &weir.MutateRequest{Resource: backend.resource(id), Action: weir.MutationAtomicTransform, Lua: program} rpc, cancel := context.WithTimeout(s.ctx, rpcTimeout) - result, err := weir.AtomicTransform(rpc, wire, opts) + result, err := mutateRecord(rpc, replica, backend.name, request) cancel() if err != nil || result.GetOutcome() != weir.MutationApplied || result.GetFailure() != nil { finished <- fmt.Errorf("cross-replica Lua: result=%v error=%v", result, err) @@ -124,9 +121,8 @@ func (s *system) testProjectionAndFailures(t *testing.T, backend *backendData) { t.Helper() target := backend.provisionTarget(t, s.ctx, "projection.entries") for n := int64(1); n <= 3; n++ { - write := target.write(t, fmt.Sprint(n), n) - opts := weir.WriteOptions{StoreName: target.name, Request: write} - result, err := s.client.Create(s.ctx, opts) + write := target.writeAs(t, weir.MutationCreate, fmt.Sprint(n), n) + result, err := mutateRecord(s.ctx, s.client, target.name, write) assertApplied(t, result, err) } if target.name == "search" { @@ -136,15 +132,15 @@ func (s *system) testProjectionAndFailures(t *testing.T, backend *backendData) { t.Fatalf("refresh projection fixture: %d %s", code, body) } } - wires := []nativeInvoke{ - replicaNative(transport(t, s.cluster.Nodes[0].Application)), - replicaNative(transport(t, s.cluster.Nodes[1].Application)), + replicas := []nativeInvoke{ + clientNative(replicaClient(t, s.cluster.Nodes[0].Application)), + clientNative(replicaClient(t, s.cluster.Nodes[1].Application)), } calls := 0 - invoke := func(ctx context.Context, opts weir.NativeOptions) (*weir.NativeResult, error) { - call := wires[calls%len(wires)] + invoke := func(ctx context.Context, call nativeCall) (*weir.NativeResult, error) { + replica := replicas[calls%len(replicas)] calls++ - return call(ctx, opts) + return replica(ctx, call) } bound := bson.D{{Key: "$gte", Value: int64(2)}} query := nativeQuery{ @@ -185,7 +181,7 @@ func (s *system) testProjectionAndFailures(t *testing.T, backend *backendData) { mongoProjection: bson.D{{Key: "n", Value: 0}}, searchSource: map[string]any{"excludes": []string{"n"}}, } - pager, err = target.nativePager(s.client.Native, exclude) + pager, err = target.nativePager(clientNative(s.client), exclude) if err != nil { t.Fatal(err) } @@ -216,9 +212,9 @@ func (s *system) testAdapterOwnedEnvelopes(t *testing.T, target *backendData) { if target.name == "mongo" { native.Resource = weir.EncodeSegment(target.mongo.Database().Name()) } - options := weir.NativeOptions{StoreName: target.name, Request: native, Consume: func(context.Context, *weir.NativeResponse, []byte) error { return nil }} + call := nativeCall{store: target.name, request: native, consume: func(context.Context, *weir.NativeResponse, []byte) error { return nil }} rpc, cancel := context.WithTimeout(s.ctx, rpcTimeout) - result, err := s.client.Native(rpc, options) + result, err := runNative(rpc, s.client, call) cancel() if err != nil || result == nil || result.Response != nil || result.Completion != weir.NativeNotStarted || result.Failure.GetCode() != weir.FailureUnsupported { t.Fatalf("unknown format must reach adapter classification: %v %v", result, err) @@ -231,7 +227,7 @@ func (s *system) testAdapterOwnedEnvelopes(t *testing.T, target *backendData) { if err != nil { t.Fatal(err) } - caller := &nativePager{store: target.name, invoke: s.client.Native} + caller := &nativePager{store: target.name, invoke: clientNative(s.client)} _, body, err := caller.exchange(s.ctx, request) raw := bson.Raw(body) if err != nil || raw.Validate() != nil { @@ -246,9 +242,8 @@ func (s *system) testAdapterOwnedEnvelopes(t *testing.T, target *backendData) { } literal := target.provisionTarget(t, s.ctx, "literal.fields") document := &weir.Document{ContentType: "application/json", Data: []byte(`{"$literal":42,"n":1}`)} - write := &weir.WriteRequest{Resource: literal.resource("literal"), Document: document} - writeOptions := weir.WriteOptions{StoreName: literal.name, Request: write} - mutation, err := s.client.Create(s.ctx, writeOptions) + write := &weir.MutateRequest{Resource: literal.resource("literal"), Action: weir.MutationCreate, Document: document} + mutation, err := mutateRecord(s.ctx, s.client, literal.name, write) assertApplied(t, mutation, err) refresh := httpRequest{method: http.MethodPost, path: "/" + literal.collection + "/_refresh"} code, body := literal.httpDo(t, s.ctx, refresh) @@ -256,7 +251,7 @@ func (s *system) testAdapterOwnedEnvelopes(t *testing.T, target *backendData) { t.Fatalf("refresh literal field: %d %s", code, body) } query := nativeQuery{searchSource: []string{"$literal"}} - pager, err := literal.nativePager(s.client.Native, query) + pager, err := literal.nativePager(clientNative(s.client), query) if err != nil { t.Fatal(err) } @@ -280,8 +275,7 @@ func (s *system) testMissingTargets(t *testing.T, target *backendData) { requests := []*weir.ReadRequest{ {Resource: target.resource("1")}, {Resource: target.resource("absent")}, {Resource: missing.resource("1")}, } - opts := weir.ReadOptions{StoreName: target.name, Requests: requests} - results, err := s.client.Read(s.ctx, opts) + results, err := readRecords(s.ctx, s.client, target.name, requests...) if err != nil || len(results) != 3 { t.Fatalf("mixed read results: %v %v", results, err) } @@ -292,7 +286,7 @@ func (s *system) testMissingTargets(t *testing.T, target *backendData) { mongoFilter: bson.D{{Key: "n", Value: int64(999)}}, searchQuery: map[string]any{"term": map[string]any{"n": 999}}, } - pager, err := target.nativePager(s.client.Native, empty) + pager, err := target.nativePager(clientNative(s.client), empty) if err != nil { t.Fatal(err) } @@ -300,7 +294,7 @@ func (s *system) testMissingTargets(t *testing.T, target *backendData) { t.Fatalf("empty Native query returned %d records", len(documents)) } query := nativeQuery{} - pager, err = missing.nativePager(s.client.Native, query) + pager, err = missing.nativePager(clientNative(s.client), query) if err != nil { t.Fatal(err) } diff --git a/tests/integration/silent_test.go b/tests/integration/silent_test.go index 1741022..b20ee9a 100644 --- a/tests/integration/silent_test.go +++ b/tests/integration/silent_test.go @@ -83,10 +83,9 @@ func (s *system) testSilentConnection(t *testing.T) { defer client.Close() put := func(n int64, timeout time.Duration) (*weir.MutationResult, error) { request := backend.write(t, "silent", n) - options := weir.WriteOptions{StoreName: backend.name, Request: request} rpc, cancel := context.WithTimeout(s.ctx, timeout) defer cancel() - return client.Put(rpc, options) + return mutateRecord(rpc, client, backend.name, request) } result, err := put(1, rpcTimeout) assertApplied(t, result, err) diff --git a/tests/integration/single_benchmark_test.go b/tests/integration/single_benchmark_test.go index c9748dc..e03f0d0 100644 --- a/tests/integration/single_benchmark_test.go +++ b/tests/integration/single_benchmark_test.go @@ -1,4 +1,4 @@ -//go:build integration +//go:build integration && !previous_sdk package integration_test diff --git a/tests/integration/soak_test.go b/tests/integration/soak_test.go index ed566ed..da50ea1 100644 --- a/tests/integration/soak_test.go +++ b/tests/integration/soak_test.go @@ -1,4 +1,4 @@ -//go:build integration +//go:build integration && !previous_sdk package integration_test diff --git a/tests/integration/stream_memory_test.go b/tests/integration/stream_memory_test.go index 5d6ca37..8c3b57c 100644 --- a/tests/integration/stream_memory_test.go +++ b/tests/integration/stream_memory_test.go @@ -120,8 +120,7 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { reads[index] = read } rpc, cancel := context.WithTimeout(s.ctx, 90*time.Second) - writeOptions := weir.MutateOptions{StoreName: backend.name, Requests: requests} - results, err := s.client.Mutate(rpc, writeOptions) + results, err := mutateRecords(rpc, s.client, backend.name, requests...) cancel() if err != nil || len(results) != distinctRecords { t.Fatal("prepare distinct stream documents", len(results), err) @@ -139,7 +138,7 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { ctx, stop := context.WithTimeout(s.ctx, 90*time.Second) produced, consumed := 0, uint64(0) // Results arrive in completion order; each index appears once. - seen := make([]bool, count+1) + seen := make([]bool, count) var bytes, peak uint64 sample := func() error { resident, err := residentBytes(ctx, s.cluster.Nodes) @@ -154,9 +153,9 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { stop() t.Fatal(err) } - options := weir.ReadStreamOptions{ - StoreName: backend.name, - Next: func(context.Context) (*weir.ReadRequest, error) { + stream := readStream{ + store: backend.name, + next: func(context.Context) (*weir.ReadRequest, error) { if produced == count { return nil, io.EOF } @@ -164,8 +163,8 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { produced++ return request, nil }, - Consume: func(_ context.Context, index uint64, result *weir.ReadResult) error { - if index == 0 || index > uint64(count) || seen[index] || result == nil || result.Document == nil || result.Failure != nil || result.Missing { + consume: func(_ context.Context, index int, result *weir.ReadResult) error { + if index < 0 || index >= count || seen[index] || result == nil || result.Document == nil || result.Failure != nil || result.Missing { return fmt.Errorf("unexpected stream result at %d", index) } seen[index] = true @@ -180,7 +179,7 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { return nil }, } - err := s.client.ReadStream(ctx, options) + err := streamReads(ctx, s.client, stream) if err == nil { err = sample() } @@ -189,7 +188,7 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { t.Fatalf("large call failed: count=%d consumed=%d bytes=%d error=%v", count, consumed, bytes, err) } s.waitStreamIdle(t, backend.name) - evidence := streamBatchEvidence{Before: before, After: s.nodeMetrics(t), Store: backend.name, Method: "read", Records: count} + evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "read", Records: count} assertStreamBatchMetrics(t, evidence) if count == 1024 { shortPeak = peak diff --git a/tests/integration/system_test.go b/tests/integration/system_test.go index c8f85e2..e2d8781 100644 --- a/tests/integration/system_test.go +++ b/tests/integration/system_test.go @@ -178,14 +178,12 @@ func (s *system) testCancellation(t *testing.T) { canceled, cancel := context.WithCancel(ctx) cancel() read := &weir.ReadRequest{Resource: backend.resource("primary")} - request := weir.ReadOneOptions{StoreName: backend.name, Request: read} - result, err := client.ReadOne(canceled, request) + result, err := readRecord(canceled, client, backend.name, read) if result != nil || !cancellation(err) { t.Fatalf("canceled Read: result=%v err=%v", result, err) } - write := backend.write(t, "canceled_create", 123) - writeOptions := weir.WriteOptions{StoreName: backend.name, Request: write} - mutation, err := client.Create(canceled, writeOptions) + write := backend.writeAs(t, weir.MutationCreate, "canceled_create", 123) + mutation, err := mutateRecord(canceled, client, backend.name, write) if mutation != nil || !cancellation(err) { t.Fatalf("canceled Create: result=%v err=%v", mutation, err) } @@ -200,10 +198,9 @@ func (s *system) testRecovery(t *testing.T) { t.Helper() ctx, cluster, client, backends := s.ctx, s.cluster, s.client, s.backends for _, backend := range backends { - write := backend.write(t, "durable", 99) - options := weir.WriteOptions{StoreName: backend.name, Request: write} + write := backend.writeAs(t, weir.MutationCreate, "durable", 99) rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - result, err := client.Create(rpc, options) + result, err := mutateRecord(rpc, client, backend.name, write) cancel() assertApplied(t, result, err) backend.assertPersisted(t, ctx, "durable", 99) @@ -227,9 +224,8 @@ func (s *system) testRecovery(t *testing.T) { want := record{id: "durable", n: 99} backend.assertRead(t, ctx, survivor, want) read := &weir.ReadRequest{Resource: backend.resource("durable")} - readOptions := weir.ReadOneOptions{StoreName: backend.name, Request: read} rpc, cancel := context.WithTimeout(ctx, rpcTimeout) - result, err := client.ReadOne(rpc, readOptions) + result, err := readRecord(rpc, client, backend.name, read) cancel() if result != nil || status.Code(err) != codes.Unavailable { t.Fatalf("read through a stopped replica: want Unavailable, got result=%v err=%v", result, err) @@ -314,6 +310,27 @@ func (s *system) nodeMetrics(t *testing.T) []observe.Snapshot { return snapshots } +// metricsAfterStream returns the nodes' metrics once a server has recorded the end +// of a stream that started after before. A call returns with its last result, which +// can reach the SDK before the server records that its stream ended. +func (s *system) metricsAfterStream(t *testing.T, before []observe.Snapshot) []observe.Snapshot { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + after := s.nodeMetrics(t) + completed := 0.0 + for index := range before { + left, _ := before[index].Sum("weir_rpc_completions_total", nil) + right, _ := after[index].Sum("weir_rpc_completions_total", nil) + completed += right - left + } + if completed != 0 || time.Now().After(deadline) { + return after + } + time.Sleep(20 * time.Millisecond) + } +} + type streamBatchEvidence struct { Before, After []observe.Snapshot Store, Method string @@ -375,9 +392,8 @@ func (s *system) testBatchSingleStream(t *testing.T, backend *backendData) { request := &weir.ReadRequest{Resource: backend.resource("primary")} reads[index] = request } - opts := weir.ReadOptions{StoreName: backend.name, Requests: reads} rpc, cancel := context.WithTimeout(s.ctx, rpcTimeout) - results, err := s.client.Read(rpc, opts) + results, err := readRecords(rpc, s.client, backend.name, reads...) cancel() if err != nil || len(results) != len(reads) { t.Fatalf("streamed batch: results=%d err=%v", len(results), err) @@ -385,6 +401,6 @@ func (s *system) testBatchSingleStream(t *testing.T, backend *backendData) { for _, result := range results { backend.assertReadResult(t, result, nil, 5) } - evidence := streamBatchEvidence{Before: before, After: s.nodeMetrics(t), Store: backend.name, Method: "read", Records: len(reads)} + evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "read", Records: len(reads)} assertStreamBatchMetrics(t, evidence) } diff --git a/versions.json b/versions.json index d819fb4..b44f3a4 100644 --- a/versions.json +++ b/versions.json @@ -1,7 +1,7 @@ { "weir_source": "5107986b882da60751403c7e05bbbebdb12b3cdb", "weir_previous_source": "3717479d2820c8e99927d4e91f7c32298e0c32b1", - "weir_go_previous": "v0.22.0", + "weir_go_previous": "v0.23.1", "mongodb_version": "8.0.32", "mongodb_image": "mongo@sha256:d0d926f94df099bff534b7ee5b5986458131a22489dfff8664509af0c1e2ca9c", "elasticsearch_version": "8.19.22", From 12ef6e0886860389ade261b1f9231166c19079b7 Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Thu, 5 Nov 2026 18:54:21 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=85=20test:=20check=20SDK=20calls=20w?= =?UTF-8?q?here=20the=20server=20receives=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SDK options cannot be read outside the SDK, so the crawler's visibility tests had fallen back to reading their own config and passed with the option removed. A recording server now checks visibility and action. --- internal/benchmark/crawler_completion_test.go | 4 +- internal/benchmark/crawler_single_test.go | 36 ++++++- internal/benchmark/partial_update_test.go | 4 +- internal/soak/soak_test.go | 31 +++++-- internal/wiretest/wiretest.go | 93 +++++++++++++++++++ internal/workload/weir_batch_test.go | 37 ++++---- 6 files changed, 168 insertions(+), 37 deletions(-) create mode 100644 internal/wiretest/wiretest.go diff --git a/internal/benchmark/crawler_completion_test.go b/internal/benchmark/crawler_completion_test.go index 862eca8..4cc81f6 100644 --- a/internal/benchmark/crawler_completion_test.go +++ b/internal/benchmark/crawler_completion_test.go @@ -45,8 +45,8 @@ func TestPartialUpdateApplicationAcknowledgedMatchesNativeAndPreservesPadding(t if err != nil || len(requests) != 1 { t.Fatal(requests, err) } - if crawlerQueryVisible(config) || requests[0].Lua != nil { - t.Fatal("Weir did not use the same application completion boundary", requests[0]) + if requests[0].Lua != nil { + t.Fatal("Weir partial update used Lua", requests[0]) } var payload map[string]any if err := json.Unmarshal(requests[0].BackendExpression.Data, &payload); err != nil || !reflect.DeepEqual(payload["doc"], mutation.Document) { diff --git a/internal/benchmark/crawler_single_test.go b/internal/benchmark/crawler_single_test.go index 2e9cee6..498f5f6 100644 --- a/internal/benchmark/crawler_single_test.go +++ b/internal/benchmark/crawler_single_test.go @@ -12,6 +12,7 @@ import ( "time" weir "github.com/batchstream/weir-go" + "github.com/batchstream/weir-tests/internal/wiretest" ) func TestCrawlerSingleNativeHasAtomicEndpointAndConcurrentSellerFanout(t *testing.T) { @@ -125,6 +126,37 @@ func TestCrawlerSingleMissingAcknowledgementStopsCausalCallsWithoutReplay(t *tes } } +func TestCrawlerCallsSendTheirCompletionOnTheWire(t *testing.T) { + tests := []struct { + name string + profile string + visibility string + mutation crawlerMutation + queryVisible bool + }{ + {name: "lua", mutation: crawlerPage(0, 1, 1)[0], queryVisible: true}, + {name: "partial update", profile: "partial-update", mutation: partialUpdateMutation(1, 2), queryVisible: true}, + {name: "partial update acknowledged", profile: "partial-update", visibility: "application_acknowledged", mutation: partialUpdateMutation(1, 2)}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + server := &wiretest.Recorder{} + config := crawlerClientConfig{InputShape: "single", Namespace: "fixture", PageSize: 1, Profile: test.profile, Visibility: test.visibility} + client := &crawlerClient{config: config, sdk: wiretest.Client(t, server)} + mutations := []crawlerMutation{test.mutation} + var ledger crawlerApplicationLedger + unknown, err := client.executeMutations(t.Context(), mutations, &ledger) + if err != nil || unknown { + t.Fatal(unknown, err) + } + received := server.Mutations() + if len(received) != 1 || wiretest.QueryVisible(received[0]) != test.queryVisible { + t.Fatalf("query visible=%v, sent %v", test.queryVisible, received) + } + }) + } +} + func TestCrawlerSingleSDKConstructsExactlyOneRecordAndRejectsBatches(t *testing.T) { config := crawlerClientConfig{InputShape: "single", Namespace: "fixture", PageSize: 1} mutation := crawlerPage(0, 1, 1)[0] @@ -134,8 +166,8 @@ func TestCrawlerSingleSDKConstructsExactlyOneRecordAndRejectsBatches(t *testing. t.Fatal("single SDK call was not one record", requests, err) } request := requests[0] - if !crawlerQueryVisible(config) || request.Action != weir.MutationAtomicTransform || request.Lua == nil || request.Lua.Input == nil { - t.Fatal("single SDK call lost Lua or completion semantics", request) + if request.Action != weir.MutationAtomicTransform || request.Lua == nil || request.Lua.Input == nil { + t.Fatal("single SDK call lost Lua", request) } var actual crawlerMutation if err := json.Unmarshal(request.Lua.Input.Data, &actual); err != nil || !reflect.DeepEqual(actual, mutation) { diff --git a/internal/benchmark/partial_update_test.go b/internal/benchmark/partial_update_test.go index c2e1c4c..a32dff8 100644 --- a/internal/benchmark/partial_update_test.go +++ b/internal/benchmark/partial_update_test.go @@ -52,8 +52,8 @@ func TestPartialUpdateNativeAndWeirUseIdenticalSingleDocWithoutLua(t *testing.T) t.Fatal(requests, err) } request := requests[0] - if request.Lua != nil || request.BackendExpression == nil || request.BackendExpression.ContentType != "application/vnd.weir.search-update.v1+json" || request.Action != weir.MutationAtomicTransform || !crawlerQueryVisible(config) { - t.Fatal("mechanism included Lua or changed completion semantics", request) + if request.Lua != nil || request.BackendExpression == nil || request.BackendExpression.ContentType != "application/vnd.weir.search-update.v1+json" || request.Action != weir.MutationAtomicTransform { + t.Fatal("mechanism included Lua", request) } var payload map[string]any if err := json.Unmarshal(request.BackendExpression.Data, &payload); err != nil || len(payload) != 1 || !reflect.DeepEqual(payload["doc"], mutation.Document) { diff --git a/internal/soak/soak_test.go b/internal/soak/soak_test.go index 54c9a40..7f2b9f3 100644 --- a/internal/soak/soak_test.go +++ b/internal/soak/soak_test.go @@ -8,13 +8,13 @@ import ( "math/rand/v2" "net/http" "net/http/httptest" - "slices" "strconv" "strings" "testing" "time" weir "github.com/batchstream/weir-go" + "github.com/batchstream/weir-tests/internal/wiretest" "go.mongodb.org/mongo-driver/v2/bson" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -82,13 +82,11 @@ type scriptedClient struct { streamMutations func(context.Context, nextMutation, consumeMutation) error streamReads func(context.Context, nextRead, consumeRead) error transforms []*weir.MutateRequest - visible []bool } // mutate answers a transform and a single Put from the script, and any other call // through mutations. -func (c *scriptedClient) mutate(_ context.Context, _ string, visible bool, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { - c.visible = append(c.visible, visible) +func (c *scriptedClient) mutate(_ context.Context, _ string, _ bool, requests ...*weir.MutateRequest) ([]*weir.MutationResult, error) { if len(requests) != 1 { return c.mutations(requests) } @@ -203,15 +201,28 @@ func TestKeyAllowsOnlyTheLatestAppliedOrAnUnknownSequence(t *testing.T) { } } -func TestOnlyTheVisibleProfileWaitsForQueryVisibility(t *testing.T) { +func TestWriteProfilesSendTheirMutationOnTheWire(t *testing.T) { + server := &wiretest.Recorder{} store := &memoryStore{seqs: map[string]int64{}} - steps := []step{{outcome: weir.MutationApplied, lands: true}, {outcome: weir.MutationApplied, lands: true}} - client := &scriptedClient{store: store, steps: steps} - load, _ := newTestWorker(store, client) + load, _ := newTestWorker(store, nil) + load.client = sdkRecords{client: wiretest.Client(t, server)} + load.storeName = "search" + load.verifyPercent = 0 load.putTurn(readyTokens(1)) load.visibleTurn(readyTokens(1)) - if !slices.Equal(client.visible, []bool{false, true}) { - t.Fatalf("visible=%v", client.visible) + load.luaTurn(readyTokens(1)) + received := server.Mutations() + if len(received) != 3 { + t.Fatalf("mutations=%d", len(received)) + } + if received[0].GetPut() == nil || wiretest.QueryVisible(received[0]) { + t.Errorf("single profile sent %v", received[0]) + } + if received[1].GetPut() == nil || !wiretest.QueryVisible(received[1]) { + t.Errorf("visible profile sent %v", received[1]) + } + if received[2].GetAtomicTransform().GetLua() == nil || wiretest.QueryVisible(received[2]) { + t.Errorf("lua profile sent %v", received[2]) } } diff --git a/internal/wiretest/wiretest.go b/internal/wiretest/wiretest.go new file mode 100644 index 0000000..c4f8e28 --- /dev/null +++ b/internal/wiretest/wiretest.go @@ -0,0 +1,93 @@ +// Package wiretest gives tests a Client connected in memory to a server of their +// own, to check what an SDK call puts on the wire. An SDK option cannot be inspected +// outside the SDK, so the server is where a test sees its effect. +package wiretest + +import ( + "context" + "errors" + "io" + "net" + "slices" + "sync" + "testing" + + weir "github.com/batchstream/weir-go" + "github.com/batchstream/weir-protocol/api/protocol" + pb "github.com/batchstream/weir-protocol/api/weir/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/grpc/test/bufconn" +) + +// Client returns a Client whose only server is server. +func Client(t *testing.T, server pb.StoreServiceServer) *weir.Client { + t.Helper() + listener := bufconn.Listen(1 << 20) + serving := grpc.NewServer() + pb.RegisterStoreServiceServer(serving, server) + done := make(chan error, 1) + go func() { done <- serving.Serve(listener) }() + t.Cleanup(func() { + serving.Stop() + listener.Close() + <-done + }) + dial := func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) } + // The address only labels the server the dialer reaches. + client, err := weir.New("127.0.0.1:1", weir.WithDialOptions(grpc.WithContextDialer(dial))) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { client.Close() }) + return client +} + +// Recorder is a server that applies every mutation and keeps what it received. +type Recorder struct { + mu sync.Mutex + mutations []*pb.MutateRequest +} + +// Execute answers each mutation as applied and refuses anything else. +func (r *Recorder) Execute(stream grpc.BidiStreamingServer[pb.ExecuteRequests, pb.ExecuteResponses]) error { + for { + message, err := stream.Recv() + if errors.Is(err, io.EOF) { + return nil + } + if err != nil { + return err + } + for _, request := range message.Requests { + mutation := request.Command.GetMutate() + if mutation == nil { + return status.Error(codes.InvalidArgument, "mutation required") + } + r.mu.Lock() + r.mutations = append(r.mutations, mutation) + r.mu.Unlock() + result := protocol.Mutation(pb.MutationOutcome_APPLIED, nil) + payload := &pb.Event_MutationResult{MutationResult: result} + event := &pb.Event{Value: payload} + response := &pb.ExecuteResponse{Index: request.Index, Event: event} + reply := &pb.ExecuteResponses{Responses: []*pb.ExecuteResponse{response}} + if err := stream.Send(reply); err != nil { + return err + } + } + } +} + +// Mutations returns the mutations received so far, in order of arrival. +func (r *Recorder) Mutations() []*pb.MutateRequest { + r.mu.Lock() + defer r.mu.Unlock() + return slices.Clone(r.mutations) +} + +// QueryVisible reports whether a mutation waits for query visibility. +func QueryVisible(mutation *pb.MutateRequest) bool { + return mutation.GetOptions().GetVisibility() == pb.MutationVisibility_QUERY_VISIBLE +} diff --git a/internal/workload/weir_batch_test.go b/internal/workload/weir_batch_test.go index 0578955..29270ec 100644 --- a/internal/workload/weir_batch_test.go +++ b/internal/workload/weir_batch_test.go @@ -3,7 +3,6 @@ package workload import ( "context" "io" - "net" "sync/atomic" "testing" "time" @@ -11,10 +10,10 @@ import ( weir "github.com/batchstream/weir-go" "github.com/batchstream/weir-protocol/api/protocol" pb "github.com/batchstream/weir-protocol/api/weir/v1" + "github.com/batchstream/weir-tests/internal/wiretest" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/grpc/test/bufconn" ) type acknowledgementPeer struct { @@ -96,7 +95,7 @@ func TestSDKStreamReadPreservesConfirmedPrefixThroughLaterFailure(t *testing.T) first := Operation{Record: 0} second := Operation{Record: 1} peer := &acknowledgementPeer{document: dataset.Document(first)} - client := mutationClient(t, peer) + client := wiretest.Client(t, peer) firstRequest := &weir.ReadRequest{Resource: dataset.Resource(first.Record)} secondRequest := &weir.ReadRequest{Resource: dataset.Resource(second.Record)} replies, err := client.Read(t.Context(), "search", weir.WithRequests(firstRequest, secondRequest)) @@ -111,23 +110,19 @@ func TestSDKStreamReadPreservesConfirmedPrefixThroughLaterFailure(t *testing.T) } } -// mutationClient returns a Client whose only server is peer, in memory. -func mutationClient(t *testing.T, peer *acknowledgementPeer) *weir.Client { - t.Helper() - listener := bufconn.Listen(1 << 20) - server := grpc.NewServer() - pb.RegisterStoreServiceServer(server, peer) - done := make(chan error, 1) - go func() { done <- server.Serve(listener) }() - t.Cleanup(func() { server.Stop(); listener.Close(); <-done }) - dial := func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) } - // The address only labels the server the dialer reaches. - client, err := weir.New("127.0.0.1:1", weir.WithDialOptions(grpc.WithContextDialer(dial))) - if err != nil { - t.Fatal(err) +func TestWeirPathWritesAPutOfTheRecord(t *testing.T) { + dataset := testDataset(t, "search") + server := &wiretest.Recorder{} + path := &weirPath{dataset: dataset, client: wiretest.Client(t, server)} + operation := Operation{Record: 3, Write: true, Revision: 1} + outcome := path.Execute(t.Context(), operation) + received := server.Mutations() + if outcome.Status != Success || len(received) != 1 { + t.Fatal("write did not apply one record", outcome, received) + } + if received[0].GetResource() != dataset.Resource(operation.Record) || received[0].GetPut() == nil { + t.Fatal("write sent another action or resource", received[0]) } - t.Cleanup(func() { client.Close() }) - return client } func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { @@ -137,7 +132,7 @@ func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { if mode == "canceled_after_apply" { peer.firstMutation = make(chan struct{}) } - client := mutationClient(t, peer) + client := wiretest.Client(t, peer) document := &weir.Document{ContentType: "application/json", Data: []byte(`{"n":1}`)} first := &weir.MutateRequest{Resource: "records/s:first", Action: weir.MutationPut, Document: document} second := &weir.MutateRequest{Resource: "records/s:second", Action: weir.MutationPut, Document: document} @@ -183,7 +178,7 @@ func TestSDKStreamMutationAcknowledgementAndNoReplay(t *testing.T) { func TestSDKStreamMutationPreflightSendsNoRecord(t *testing.T) { peer := &acknowledgementPeer{mode: "applied_failure"} - client := mutationClient(t, peer) + client := wiretest.Client(t, peer) document := &weir.Document{ContentType: "application/json", Data: []byte(`{"n":1}`)} first := &weir.MutateRequest{Resource: "records/s:first", Action: weir.MutationPut, Document: document} invalid := &weir.MutateRequest{Resource: "records/s:second", Action: 0, Document: document} From 69b9642e07fafe2e1ea5fd9dc46be30ade2c0ba2 Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Thu, 5 Nov 2026 18:54:21 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=85=20test:=20read=20stream=20metrics?= =?UTF-8?q?=20once=20every=20stream=20has=20ended?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Waiting for the first recorded completion could miss a call that split across streams. Waiting for no business RPC sees all of them. --- tests/integration/batch_test.go | 4 +-- tests/integration/soak_test.go | 8 +++--- tests/integration/stream_memory_test.go | 4 +-- tests/integration/system_test.go | 38 +++++++++---------------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/tests/integration/batch_test.go b/tests/integration/batch_test.go index 95454c1..38ebe0d 100644 --- a/tests/integration/batch_test.go +++ b/tests/integration/batch_test.go @@ -270,7 +270,7 @@ func (s *system) testLargeDistinctBatch(t *testing.T, backend *backendData) { for _, result := range results { assertApplied(t, result, nil) } - evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "mutate", Records: count} + evidence := streamBatchEvidence{Before: before, After: s.metricsAtRest(t), Store: backend.name, Method: "mutate", Records: count} assertStreamBatchMetrics(t, evidence) for index := range mutations { backend.assertPersisted(t, s.ctx, fmt.Sprintf("large_distinct_%03d", index), int64(10000+index)) @@ -285,7 +285,7 @@ func (s *system) testLargeDistinctBatch(t *testing.T, backend *backendData) { for index, result := range readResults { backend.assertReadResult(t, result, nil, int64(10000+count-1-index)) } - evidence.Before, evidence.After, evidence.Method = before, s.metricsAfterStream(t, before), "read" + evidence.Before, evidence.After, evidence.Method = before, s.metricsAtRest(t), "read" assertStreamBatchMetrics(t, evidence) t.Log("513 distinct native documents Put and Read in aggregation groups on one streamed call; every value independently persisted and reverse-order results verified") } diff --git a/tests/integration/soak_test.go b/tests/integration/soak_test.go index da50ea1..fbd7297 100644 --- a/tests/integration/soak_test.go +++ b/tests/integration/soak_test.go @@ -14,10 +14,10 @@ import ( "github.com/batchstream/weir-tests/internal/soak" ) -// TestSoakEverySDKCall runs the soak workload with its default profiles against -// real backends without faults: every SDK call must succeed, without an error, -// and every outcome must match the databases. -func TestSoakEverySDKCall(t *testing.T) { +// TestSoakEveryProfile runs the soak workload with its default profiles against +// real backends without faults: every call must succeed, without an error, and +// every outcome must match the databases. +func TestSoakEveryProfile(t *testing.T) { binary := os.Getenv("WEIR_TEST_BINARY") if binary == "" { t.Fatal("integration tests require WEIR_TEST_BINARY pointing to a built Weir binary") diff --git a/tests/integration/stream_memory_test.go b/tests/integration/stream_memory_test.go index 8c3b57c..9ef4d2f 100644 --- a/tests/integration/stream_memory_test.go +++ b/tests/integration/stream_memory_test.go @@ -23,7 +23,7 @@ import ( // streamPendingRecords is Weir's fixed per-stream transport window. const streamPendingRecords = 32 -// A ReadStream uses one Execute RPC with no other business RPCs active here. +// A streamed Read uses one Execute RPC with no other business RPCs active here. // The stream's transport window bounds the records it can have queued. func streamReservations(ctx context.Context, nodes []fixture.Node, store string) (bool, error) { idle := true @@ -188,7 +188,7 @@ func (s *system) testStreamMemory(t *testing.T, backend *backendData) { t.Fatalf("large call failed: count=%d consumed=%d bytes=%d error=%v", count, consumed, bytes, err) } s.waitStreamIdle(t, backend.name) - evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "read", Records: count} + evidence := streamBatchEvidence{Before: before, After: s.metricsAtRest(t), Store: backend.name, Method: "read", Records: count} assertStreamBatchMetrics(t, evidence) if count == 1024 { shortPeak = peak diff --git a/tests/integration/system_test.go b/tests/integration/system_test.go index e2d8781..a6ad4b4 100644 --- a/tests/integration/system_test.go +++ b/tests/integration/system_test.go @@ -278,10 +278,19 @@ func (s *system) quiesce(t *testing.T) { t.Fatal("create SDK client:", err) } s.client = client + s.metricsAtRest(t) +} + +// metricsAtRest returns the nodes' metrics once they hold no business RPC. A call +// returns with its last result, which can reach the SDK before the server ends the +// call's streams, and a server counts a completed RPC before it stops holding it. +func (s *system) metricsAtRest(t *testing.T) []observe.Snapshot { + t.Helper() deadline := time.Now().Add(5 * time.Second) for { + snapshots := s.nodeMetrics(t) sessions := 0.0 - for index, snapshot := range s.nodeMetrics(t) { + for index, snapshot := range snapshots { value, found := snapshot.Sum("weir_ingress_sessions", nil) if !found { t.Fatalf("node %d session observation unavailable", index) @@ -289,10 +298,10 @@ func (s *system) quiesce(t *testing.T) { sessions += value } if sessions == 0 { - return + return snapshots } if time.Now().After(deadline) { - t.Fatalf("closed SDK left %v sessions open", sessions) + t.Fatalf("SDK left %v sessions open", sessions) } time.Sleep(20 * time.Millisecond) } @@ -310,27 +319,6 @@ func (s *system) nodeMetrics(t *testing.T) []observe.Snapshot { return snapshots } -// metricsAfterStream returns the nodes' metrics once a server has recorded the end -// of a stream that started after before. A call returns with its last result, which -// can reach the SDK before the server records that its stream ended. -func (s *system) metricsAfterStream(t *testing.T, before []observe.Snapshot) []observe.Snapshot { - t.Helper() - deadline := time.Now().Add(5 * time.Second) - for { - after := s.nodeMetrics(t) - completed := 0.0 - for index := range before { - left, _ := before[index].Sum("weir_rpc_completions_total", nil) - right, _ := after[index].Sum("weir_rpc_completions_total", nil) - completed += right - left - } - if completed != 0 || time.Now().After(deadline) { - return after - } - time.Sleep(20 * time.Millisecond) - } -} - type streamBatchEvidence struct { Before, After []observe.Snapshot Store, Method string @@ -401,6 +389,6 @@ func (s *system) testBatchSingleStream(t *testing.T, backend *backendData) { for _, result := range results { backend.assertReadResult(t, result, nil, 5) } - evidence := streamBatchEvidence{Before: before, After: s.metricsAfterStream(t, before), Store: backend.name, Method: "read", Records: len(reads)} + evidence := streamBatchEvidence{Before: before, After: s.metricsAtRest(t), Store: backend.name, Method: "read", Records: len(reads)} assertStreamBatchMetrics(t, evidence) } From 56930b517b139dec43b1974389da15085a2b01e8 Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Thu, 5 Nov 2026 18:54:21 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9D=20docs:=20state=20what=20the?= =?UTF-8?q?=20soak=20and=20previous=20SDK=20runs=20do=20not=20cover?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/ci.md | 7 +++++-- docs/soak.md | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/ci.md b/docs/ci.md index 50bae9d..2599503 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -30,8 +30,11 @@ behaviour too. The SDK's API changed at v0.24.0, so `integration-previous-sdk` also sets the `previous_sdk` tag. `sdk_current_test.go` and `sdk_previous_test.go` hold the calls that differ. The soak and benchmark tests use packages built on the current API -only, so they do not run on the previous SDK. When `weir_go_previous` reaches -v0.24.0, delete the tag, `sdk_previous_test.go` and the constraints on those tests. +only, so they do not run on the previous SDK, and neither does the native write +evidence subtest, which lives with them. That leaves the previous SDK's `MutateStream` +and query-visible writes untested against a newer server. When `weir_go_previous` +reaches v0.24.0, delete the tag, `sdk_previous_test.go` and the constraints on those +tests. ## Output diff --git a/docs/soak.md b/docs/soak.md index 9708ba0..2e16dae 100644 --- a/docs/soak.md +++ b/docs/soak.md @@ -12,7 +12,7 @@ fresh MongoDB database and Search index, drops them at the end unless ## Profiles Each worker uses the SDK in one way, its profile, and `-profiles` sets their -shares of a backend's workers. The default exercises every call: +shares of a backend's workers. The default runs all of them: | Profile | Writes | Reads | | --- | --- | --- |