|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package workunit_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "google.golang.org/protobuf/testing/protocmp" |
| 25 | + essmsgpb "xmanager_cloud/api/protos/experiment" |
| 26 | + "xmanager_cloud/internal/grpcerr" |
| 27 | + "xmanager_cloud/internal/storage/spanner" |
| 28 | + "xmanager_cloud/internal/storage/spanner/emulator" |
| 29 | + storagetest "xmanager_cloud/internal/storage/testing" |
| 30 | +) |
| 31 | + |
| 32 | +func newEmulatorStorage(ctx context.Context, t *testing.T) *spanner.Storage { |
| 33 | + t.Helper() |
| 34 | + _, essClient := emulator.Setup(ctx, t) |
| 35 | + store, err := spanner.NewStorage(essClient) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("spanner.NewStorage failed: %v", err) |
| 38 | + } |
| 39 | + return store |
| 40 | +} |
| 41 | + |
| 42 | +func seedExperimentWithCreator(ctx context.Context, t *testing.T, store *spanner.Storage, xid int64, creator string) { |
| 43 | + t.Helper() |
| 44 | + exp := spanner.BlankExperimentProto(xid) |
| 45 | + exp.Creator = creator |
| 46 | + _ = spanner.CreateAndStoreExperiment(ctx, t, store, exp) |
| 47 | +} |
| 48 | + |
| 49 | +func seedActiveResourceWorkUnits(ctx context.Context, t *testing.T, store *spanner.Storage, specs []storagetest.ActiveResourceSpec) { |
| 50 | + t.Helper() |
| 51 | + wus := make([]*essmsgpb.WorkUnit, 0, len(specs)) |
| 52 | + for _, spec := range specs { |
| 53 | + wu := spanner.BlankWorkUnitProto(spec.XID, spec.WID) |
| 54 | + wu.CurrentWorkUnitExecution.Status.State = spec.State |
| 55 | + wu.Executable = spec.Executable() |
| 56 | + wus = append(wus, wu) |
| 57 | + } |
| 58 | + insertWorkUnitsAt(ctx, t, store, time.Date(2026, 2, 1, 12, 0, 0, 0, time.UTC), wus) |
| 59 | +} |
| 60 | + |
| 61 | +func TestFetchActiveWorkUnitResources_GuardsAndEmpty(t *testing.T) { |
| 62 | + if _, err := (&spanner.Storage{}).FetchActiveWorkUnitResources(t.Context(), []string{"q"}); !errors.Is(err, grpcerr.ErrFailedPrecondition) { |
| 63 | + t.Errorf("FetchActiveWorkUnitResources(nil client) = %v, want ErrFailedPrecondition", err) |
| 64 | + } |
| 65 | + |
| 66 | + ctx := t.Context() |
| 67 | + store := newEmulatorStorage(ctx, t) |
| 68 | + got, err := store.FetchActiveWorkUnitResources(ctx, nil) |
| 69 | + if err != nil || len(got) != 0 { |
| 70 | + t.Errorf("FetchActiveWorkUnitResources(nil queues) = (%v, %v), want (empty, nil)", got, err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestFetchActiveWorkUnitResources_AllActiveWorkUnits(t *testing.T) { |
| 75 | + ctx := t.Context() |
| 76 | + store := newEmulatorStorage(ctx, t) |
| 77 | + seedExperimentWithCreator(ctx, t, store, 201, "alice@example.com") |
| 78 | + seedExperimentWithCreator(ctx, t, store, 202, "bob@example.com") |
| 79 | + specs := storagetest.ActiveResourceSpecs(201, 202) |
| 80 | + seedActiveResourceWorkUnits(ctx, t, store, specs) |
| 81 | + |
| 82 | + got, err := store.FetchActiveWorkUnitResources(ctx, []string{storagetest.ActiveResourceQueue}) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("FetchActiveWorkUnitResources failed: %v", err) |
| 85 | + } |
| 86 | + want := storagetest.WantActiveWorkUnitResources(specs, map[int64]string{201: "alice@example.com", 202: "bob@example.com"}) |
| 87 | + if diff := cmp.Diff(want, got, storagetest.ActiveWorkUnitResourcesOrder, protocmp.Transform()); diff != "" { |
| 88 | + t.Errorf("FetchActiveWorkUnitResources() mismatch (-want +got):\n%s", diff) |
| 89 | + } |
| 90 | +} |
0 commit comments