Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,61 @@ make clean # Clean Bazel cache
**Add new entity:**
1. Create `entity/{domain}/{entity}.go` with test file and `BUILD.bazel`

**Add gomock for an extension interface:**

Mocks use the Bazel `gomock` rule from `@rules_go//extras:gomock.bzl` and are generated at build time (not checked in). See `extension/storage/mock/` for the canonical example.

To add a mock for a new interface file in an existing mock package (e.g., `extension/storage/new_store.go`):

1. Add a `//go:generate` directive to the interface file:
```go
//go:generate mockgen -source=new_store.go -destination=mock/new_store.go -package=mock
```
2. Add the file to `exports_files` in the parent `BUILD.bazel` with visibility to the mock package:
```starlark
exports_files(
["new_store.go", ...],
visibility = ["//extension/storage/mock:__pkg__"],
)
```
3. Add a `gomock` rule in the mock `BUILD.bazel`:
```starlark
gomock(
name = "mock_new_store_src",
out = "new_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:new_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)
```
4. Add the rule target to the `go_library` srcs in the same file.

To create a mock package for a new extension (e.g., `extension/queue/mock/`):

1. Create `extension/{ext}/mock/BUILD.bazel` with `gomock` rules, a `go_library`, and `# gazelle:ignore`.
2. Add `exports_files` to `extension/{ext}/BUILD.bazel` for each interface file.
3. Follow the same per-interface pattern as above.

Mock `BUILD.bazel` files use `# gazelle:ignore` so `make gazelle` will not update them — they must be maintained manually.

**Using mocks in tests:**
```go
import storagemock "github.com/uber/submitqueue/extension/storage/mock"

ctrl := gomock.NewController(t)
mockStore := storagemock.NewMockRequestStore(ctrl)
mockStore.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil)
```

Test `BUILD.bazel` deps:
```starlark
deps = [
"//extension/storage/mock",
"@org_uber_go_mock//gomock",
]
```

### Testing

- **Table-driven tests** — prefer table-driven tests with `t.Run` subtests over individual test functions.
Expand Down
13 changes: 13 additions & 0 deletions extension/storage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
load("@rules_go//go:def.bzl", "go_library")

exports_files(
[
"batch_dependent_store.go",
"batch_store.go",
"build_store.go",
"change_provider_store.go",
"request_store.go",
"speculation_tree_store.go",
"storage.go",
],
visibility = ["//extension/storage/mock:__pkg__"],
)

go_library(
name = "storage",
srcs = [
Expand Down
2 changes: 2 additions & 0 deletions extension/storage/batch_dependent_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=batch_dependent_store.go -destination=mock/batch_dependent_store.go -package=mock

import (
"context"

Expand Down
2 changes: 2 additions & 0 deletions extension/storage/batch_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=batch_store.go -destination=mock/batch_store.go -package=mock

import (
"context"

Expand Down
2 changes: 2 additions & 0 deletions extension/storage/build_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=build_store.go -destination=mock/build_store.go -package=mock

import (
"context"

Expand Down
2 changes: 2 additions & 0 deletions extension/storage/change_provider_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=change_provider_store.go -destination=mock/change_provider_store.go -package=mock

import (
"context"

Expand Down
88 changes: 88 additions & 0 deletions extension/storage/mock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
load("@rules_go//extras:gomock.bzl", "gomock")
Comment thread
manjari25 marked this conversation as resolved.
load("@rules_go//go:def.bzl", "go_library")

_MOCKGEN = "@org_uber_go_mock//mockgen"

gomock(
name = "mock_storage_src",
out = "storage_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:storage.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_request_store_src",
out = "request_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:request_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_change_provider_store_src",
out = "change_provider_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:change_provider_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_batch_store_src",
out = "batch_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:batch_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_batch_dependent_store_src",
out = "batch_dependent_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:batch_dependent_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_build_store_src",
out = "build_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:build_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

gomock(
name = "mock_speculation_tree_store_src",
out = "speculation_tree_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:speculation_tree_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)

# gazelle:ignore
go_library(
name = "mock",
srcs = [
":mock_batch_dependent_store_src",
":mock_batch_store_src",
":mock_build_store_src",
":mock_change_provider_store_src",
":mock_request_store_src",
":mock_speculation_tree_store_src",
":mock_storage_src",
],
importpath = "github.com/uber/submitqueue/extension/storage/mock",
visibility = ["//visibility:public"],
deps = [
"//entity",
"//extension/storage",
"@org_uber_go_mock//gomock",
],
)
42 changes: 42 additions & 0 deletions extension/storage/mock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Storage Mocks

Generated mocks for all `extension/storage` interfaces using [gomock](https://github.com/uber-go/mock).

Mocks are **not checked in** — they are generated at build time by the Bazel `gomock` rule.

## Adding a new store interface

When a new store interface file is added to `extension/storage/`:

1. Add a `//go:generate` directive to the new file:
```go
//go:generate mockgen -source=new_store.go -destination=mock/new_store.go -package=mock
```

2. Add the file to `exports_files` in `extension/storage/BUILD.bazel`.

3. Add a new `gomock` rule in `extension/storage/mock/BUILD.bazel`:
```starlark
gomock(
name = "mock_new_store_src",
out = "new_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:new_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)
```

4. Add the new rule target to the `go_library` srcs in the same file:
```starlark
go_library(
name = "mock",
srcs = [
...
":mock_new_store_src",
],
...
)
```

> **Note:** This BUILD.bazel uses `# gazelle:ignore`, so gazelle will not update it automatically.
2 changes: 2 additions & 0 deletions extension/storage/request_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=request_store.go -destination=mock/request_store.go -package=mock

import (
"context"

Expand Down
2 changes: 2 additions & 0 deletions extension/storage/speculation_tree_store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=speculation_tree_store.go -destination=mock/speculation_tree_store.go -package=mock

import (
"context"

Expand Down
2 changes: 2 additions & 0 deletions extension/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package storage

//go:generate mockgen -source=storage.go -destination=mock/storage.go -package=mock

import "errors"
import "fmt"

Expand Down
3 changes: 2 additions & 1 deletion gateway/controller/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ go_test(
deps = [
"//entity",
"//entity/queue",
"//extension/storage",
"//extension/storage/mock",
"//gateway/protopb",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@com_github_uber_go_tally_v4//:tally",
"@org_uber_go_mock//gomock",
"@org_uber_go_zap//:zap",
],
)
Loading