gemini-code-assist[bot] commented on code in PR #38979: URL: https://github.com/apache/beam/pull/38979#discussion_r3418629130
########## sdks/go/pkg/beam/core/runtime/harness/sampler_test.go: ########## @@ -0,0 +1,80 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package harness + +import ( + "context" + "testing" + "time" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/metrics" +) + +func TestNewSampler(t *testing.T) { + ctx := metrics.SetBundleID(context.Background(), "test-bundle") + store := metrics.GetStore(ctx) + if store == nil { + t.Fatal("GetStore returned nil") + } + + s := newSampler(store, 0) + if s == nil { + t.Fatal("newSampler returned nil") + } + if s.done == nil { + t.Error("sampler done channel is nil") + } +} + +func TestStateSampler_Stop(t *testing.T) { + ctx := metrics.SetBundleID(context.Background(), "test-bundle") + store := metrics.GetStore(ctx) + s := newSampler(store, 0) + // stop should not panic when called on a properly initialized sampler. + s.stop() +} + +func TestStateSampler_Start_ContextCancel(t *testing.T) { + ctx := metrics.SetBundleID(context.Background(), "test-bundle") + store := metrics.GetStore(ctx) + s := newSampler(store, 0) + + cancelCtx, cancel := context.WithCancel(ctx) + cancel() // Cancel immediately + + // start should return nil immediately when context is already canceled. + err := s.start(cancelCtx, samplePeriod) + if err != nil { + t.Errorf("start returned error on canceled context: %v", err) + } Review Comment:  In Go, functions that block on context cancellation typically return `ctx.Err()` (which is `context.Canceled` in this case). If `s.start` follows this standard pattern, then `err` will be non-nil, causing this test to fail. Please verify if `s.start` returns `context.Canceled` on cancellation, and if so, update the assertion to allow or explicitly assert `context.Canceled` using `errors.Is(err, context.Canceled)`. ########## sdks/go/pkg/beam/core/runtime/harness/harness_test.go: ########## @@ -258,3 +277,112 @@ func TestElementProcessingTimeoutParsing(t *testing.T) { } } } + +func TestControl_MetStoreToString(t *testing.T) { + ctx := metrics.SetBundleID(context.Background(), "test-bundle") + store := metrics.GetStore(ctx) + if store == nil { + t.Fatal("GetStore returned nil") + } + ctrl := &control{ + metStore: map[instructionID]*metrics.Store{ + "inst1": store, + }, + } + b := &strings.Builder{} + ctrl.metStoreToString(b) + out := b.String() + if !strings.Contains(out, "Bundle ID: inst1") { + t.Errorf("metStoreToString output missing bundle ID, got: %s", out) + } +} + +func TestControl_GetPlanOrResponse(t *testing.T) { + tests := []struct { + name string + active map[instructionID]*exec.Plan + awaitFinalize map[instructionID]awaitingFinalization + failed map[instructionID]error + inactive circleBuffer + wantErr bool // response has Error field set (non-nil response) + wantNilPlan bool // response is non-nil and plan is nil -> response is returned + wantEmpty bool // both plan and response are nil - empty response needed + }{ + { + name: "active", + active: map[instructionID]*exec.Plan{ + "ref": {}, + }, + }, + { + name: "awaitingFinalization", + awaitFinalize: map[instructionID]awaitingFinalization{ + "ref": {plan: &exec.Plan{}}, + }, + }, + { + name: "failed", + failed: map[instructionID]error{"ref": fmt.Errorf("test failure")}, + wantErr: true, + }, + { + name: "inactive", + inactive: func() circleBuffer { + c := newCircleBuffer() + c.Add("ref") + return c + }(), + wantEmpty: true, + }, + { + name: "notFound", + wantErr: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctrl := &control{ + active: make(map[instructionID]*exec.Plan), + awaitingFinalization: make(map[instructionID]awaitingFinalization), + failed: make(map[instructionID]error), + inactive: newCircleBuffer(), + } + if test.active != nil { + ctrl.active = test.active + } + if test.awaitFinalize != nil { + ctrl.awaitingFinalization = test.awaitFinalize + } + if test.failed != nil { + ctrl.failed = test.failed + } + if len(test.inactive.buf) > 0 { + ctrl.inactive = test.inactive + } Review Comment:  Instead of checking the length of the unexported `buf` field of `circleBuffer` to decide whether to assign it, you can directly assign `ctrl.inactive = test.inactive` unconditionally. The zero-value of `circleBuffer` is safe to assign and this avoids coupling the test runner to the internal implementation details of `circleBuffer`. ```go ctrl.inactive = test.inactive ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
