Files
tasks/internal/effect/buffer_test.go
Matt McShaneandGitHub 9a1d30b245 Refactor the Update type to a state machine (#4297)
This is a step in the direction of being able to process messages one at
a time rather than in a batch. Individual message processing will be
required to respect protocols (like update) that need message processing
that is ordered with respect to other commands. Additionally we lay the
groundwork for async updates and outcome polling.

A summary of what changed:

- Write update.Update as a protocol state machine that encapsulates all
  message processing, validation, event writing, and metric emission
  related to an update.
- Make explicit the set of "Provisional" state transitions wherein the
  Update has recieved and reacted to a protocol message but cannot make
  externally observable changes like returning update outcomes to RPC
  callers until it knows that changes have been written to stable
  storage.
- The effect package is introduced as a utility to buffer and later
  apply or cancel a set of effects.
- With more responsibility in the Update type, UpdateRegistry is
  simplified.
- UpdateRegistry can restore its state (mainly in-flight Accepted
  updates) from MutableState.
- Update message validation is moved into the update package.
- UpdateRegistry differentiates between having incomplete updates (which
  prevents a workflow from closing) and having messages to send (which
  may trigger the creation of a new workflow task)
- Removed a chunk of processing from workflowTaskHandler that is now
  encapsulated in the Update state machine, allowing task handling to be
  more decoupled from update protocol semantics.
2023-05-10 12:14:02 -07:00

94 lines
3.0 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package effect_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.temporal.io/server/internal/effect"
)
func TestBufferApplyOrder(t *testing.T) {
var buf effect.Buffer
state := make([]int, 0, 3)
buf.OnAfterCommit(func(context.Context) { state = append(state, 0) })
buf.OnAfterCommit(func(context.Context) { state = append(state, 1) })
buf.OnAfterCommit(func(context.Context) { state = append(state, 2) })
buf.Apply(context.TODO())
require.ElementsMatch(t, []int{0, 1, 2}, state)
}
func TestBufferRollbackOrder(t *testing.T) {
var buf effect.Buffer
state := make([]int, 0, 3)
buf.OnAfterRollback(func(context.Context) { state = append(state, 0) })
buf.OnAfterRollback(func(context.Context) { state = append(state, 1) })
buf.OnAfterRollback(func(context.Context) { state = append(state, 2) })
buf.Cancel(context.TODO())
require.ElementsMatch(t, []int{2, 1, 0}, state)
}
func TestBufferCancelAfterApply(t *testing.T) {
var buf effect.Buffer
var commit, rollback int
buf.OnAfterCommit(func(context.Context) { commit++ })
buf.OnAfterRollback(func(context.Context) { rollback++ })
buf.Apply(context.TODO())
buf.Cancel(context.TODO())
buf.Apply(context.TODO())
buf.Cancel(context.TODO())
buf.Apply(context.TODO())
buf.Cancel(context.TODO())
require.Equal(t, commit, 1)
require.Equal(t, rollback, 0)
}
func TestBufferApplyAfterCancel(t *testing.T) {
var buf effect.Buffer
var commit, rollback int
buf.OnAfterCommit(func(context.Context) { commit++ })
buf.OnAfterRollback(func(context.Context) { rollback++ })
buf.Cancel(context.TODO())
buf.Apply(context.TODO())
buf.Apply(context.TODO())
buf.Cancel(context.TODO())
buf.Apply(context.TODO())
buf.Cancel(context.TODO())
require.Equal(t, commit, 0)
require.Equal(t, rollback, 1)
}