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
1 change: 1 addition & 0 deletions entity/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 23 additions & 1 deletion entity/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<queue>/batch/<counter_value>".
Expand Down
30 changes: 30 additions & 0 deletions entity/batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package entity

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBatchState_IsTerminal(t *testing.T) {
Comment thread
manjari25 marked this conversation as resolved.
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())
})
}
}