diff --git a/entity/BUILD.bazel b/entity/BUILD.bazel index 98ed2138..14bca346 100644 --- a/entity/BUILD.bazel +++ b/entity/BUILD.bazel @@ -18,6 +18,7 @@ go_library( go_test( name = "entity_test", srcs = [ + "batch_test.go", "build_test.go", "queue_config_test.go", "request_test.go", diff --git a/entity/batch.go b/entity/batch.go index 91e102aa..fbe155c7 100644 --- a/entity/batch.go +++ b/entity/batch.go @@ -6,9 +6,31 @@ type BatchState string const ( // BatchStateUnknown is the unreachable state. It is set by default when the structure is initialized. It should never be seen in the system. BatchStateUnknown BatchState = "" - // TODO: Add comprehensive list of known batch states. + // BatchStateCreated is the state of a batch that has been created for processing. + BatchStateCreated BatchState = "created" + // BatchStateSpeculating is the state of a batch that is undergoing speculative execution. + BatchStateSpeculating BatchState = "speculating" + // BatchStateFinalizing is the state of a batch that is being finalized after speculative execution. + BatchStateFinalizing BatchState = "finalizing" + // BatchStateSucceeded is the terminal state of a batch that has been successfully landed. + BatchStateSucceeded BatchState = "succeeded" + // BatchStateFailed is the terminal state of a batch that has failed. + BatchStateFailed BatchState = "failed" + // BatchStateCancelled is the terminal state of a batch that was cancelled before completion. + BatchStateCancelled BatchState = "cancelled" ) +// IsTerminal returns true if the batch state is a terminal state. +// Terminal states are states from which no further transitions are possible. +func (s BatchState) IsTerminal() bool { + switch s { + case BatchStateSucceeded, BatchStateFailed, BatchStateCancelled: + return true + default: + return false + } +} + // Batch represents a group of requests to land (merge into target branch of the source control repository). type Batch struct { // ID is the globally unique identifier for the batch. Format: "/batch/". diff --git a/entity/batch_test.go b/entity/batch_test.go new file mode 100644 index 00000000..084e4b21 --- /dev/null +++ b/entity/batch_test.go @@ -0,0 +1,30 @@ +package entity + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBatchState_IsTerminal(t *testing.T) { + tests := []struct { + name string + state BatchState + terminal bool + }{ + {name: "unknown", state: BatchStateUnknown, terminal: false}, + {name: "created", state: BatchStateCreated, terminal: false}, + {name: "speculating", state: BatchStateSpeculating, terminal: false}, + {name: "finalizing", state: BatchStateFinalizing, terminal: false}, + {name: "succeeded", state: BatchStateSucceeded, terminal: true}, + {name: "failed", state: BatchStateFailed, terminal: true}, + {name: "cancelled", state: BatchStateCancelled, terminal: true}, + {name: "arbitrary string", state: BatchState("something_else"), terminal: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.terminal, tt.state.IsTerminal()) + }) + } +}