Skip to content

Commit d7900e1

Browse files
committed
feat(entities) Update batch state enum with more states
1 parent 753235b commit d7900e1

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

entity/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
go_test(
1919
name = "entity_test",
2020
srcs = [
21+
"batch_test.go",
2122
"build_test.go",
2223
"queue_config_test.go",
2324
"request_test.go",

entity/batch.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,33 @@ type BatchState string
66
const (
77
// BatchStateUnknown is the unreachable state. It is set by default when the structure is initialized. It should never be seen in the system.
88
BatchStateUnknown BatchState = ""
9-
// TODO: Add comprehensive list of known batch states.
9+
// BatchStateScheduled is the state of a batch that has been scheduled for processing.
10+
BatchStateScheduled BatchState = "scheduled"
11+
// BatchStateSpeculating is the state of a batch that is undergoing speculative execution.
12+
BatchStateSpeculating BatchState = "speculating"
13+
// BatchStateFinalizing is the state of a batch that is being finalized after speculative execution.
14+
BatchStateFinalizing BatchState = "finalizing"
15+
// BatchStateSucceeded is the terminal state of a batch that has been successfully landed.
16+
BatchStateSucceeded BatchState = "succeeded"
17+
// BatchStateFailed is the terminal state of a batch that has failed to land.
18+
BatchStateFailed BatchState = "failed"
19+
// BatchStateCancelled is the terminal state of a batch that was cancelled before completion.
20+
BatchStateCancelled BatchState = "cancelled"
21+
// BatchStateCancellationFailed is the terminal state of a batch whose cancellation process itself failed.
22+
BatchStateCancellationFailed BatchState = "cancellationfailed"
1023
)
1124

25+
// IsTerminal returns true if the batch state is a terminal state.
26+
// Terminal states are states from which no further transitions are possible.
27+
func (s BatchState) IsTerminal() bool {
28+
switch s {
29+
case BatchStateSucceeded, BatchStateFailed, BatchStateCancelled, BatchStateCancellationFailed:
30+
return true
31+
default:
32+
return false
33+
}
34+
}
35+
1236
// Batch represents a group of requests to land (merge into target branch of the source control repository).
1337
type Batch struct {
1438
// ID is the globally unique identifier for the batch. Format: "<queue>/batch/<counter_value>".

entity/batch_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package entity
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBatchState_IsTerminal(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
state BatchState
13+
terminal bool
14+
}{
15+
{name: "unknown", state: BatchStateUnknown, terminal: false},
16+
{name: "scheduled", state: BatchStateScheduled, terminal: false},
17+
{name: "speculating", state: BatchStateSpeculating, terminal: false},
18+
{name: "finalizing", state: BatchStateFinalizing, terminal: false},
19+
{name: "succeeded", state: BatchStateSucceeded, terminal: true},
20+
{name: "failed", state: BatchStateFailed, terminal: true},
21+
{name: "cancelled", state: BatchStateCancelled, terminal: true},
22+
{name: "cancellationfailed", state: BatchStateCancellationFailed, terminal: true},
23+
{name: "arbitrary string", state: BatchState("something_else"), terminal: false},
24+
}
25+
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
assert.Equal(t, tt.terminal, tt.state.IsTerminal())
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)