mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
**What changed?** gogo/protobuf has been replaced with Google's official go compiler. **Why?** gogo/protobuf has been deprecated for some time and the community is moving on, building new tools (like vtproto) atop google's v2 compiler. **How did you test it?** `make test` **Potential risks** 1. The change from embedded gogo-generated-structs to google-generated-pointers-to-structs created a risk of nil pointer exceptions. I've fixed all the ones our tests found but it's possible there are more lurking in the new code. 2. This change may cause our performance to decrease. Certainly encoding/deconding of proto objects will become slower, but the overuse of pointers by the google compiler may negatively affect our overall performance. We'll need to keep an eye on the GC stats 3. This breaks the HTTP API. We will not support [shortand payload encoding](https://github.com/temporalio/proposals/blob/master/api/http-api.md#payload-formatting) in this first pass; that will come once this initial work is in testing. **Breaking changes for developers** - `*time.Time` in proto structs will now be [timestamppb.Timestamp](https://pkg.go.dev/google.golang.org/[email protected]/types/known/timestamppb#section-documentation) - `*time.Duration` will now be [durationpb.Duration](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb) - V2-generated structs embed locks, so you cannot dereference them. `go vet` will scream at you about this. If you need a copy, use `proto.Clone`. - If the performance of this sucks then I will either update our code generator to add shallow-clone methods or hand-roll the ones we need - Proto enums will, when formatted to JSON, now be in `SCREAMING_SNAKE_CASE` rather than `PascalCase`. We decided (in discussion with the SDK team) that now was as good a time as any to rip the bandage off. - Proto objects, or objects embedding protos, cannot be compared using `reflect.DeepEqual` or _anything_ that uses it. This includes `testify` and `mock` equality testers! - You will need to use the `common/testing/protorequire`, `common/testing/protoassert`, or `common/testing/protomock` packages instead. I've implemented proto-compatible matchers and assertions there for all cases I've encountered - If you need `reflect.DeepEqual` for any reason you can use `go.temporal.io/api/temporalproto.DeepEqual` instead Note that history loading will not be impacted by the JSON changes: I rewrote history loading to dynamically fix incoming history JSON data (like all our other sdks); you can find this code in [my fork of our go API](https://github.com/tdeebswihart/temporal-api-go/blob/master/internal/temporalhistoryv1/load.go) alongside its tests. **🚨Sharp Edges Introduced🚨** Beware `*timestamppb.Timestamp.AsTime()`. If you need to extract a time value from a proto time (timestamppb) **always** make sure to check whether it's nil first. When the proto object is `nil` `AsTime()` will return a non-zero time at the proto epoch: UTC midnight on January 1, 1970. I've made this mistake multiple times during this transition and each time it's been a pain to debug **Is hotfix candidate?** No.
302 lines
9.4 KiB
Go
302 lines
9.4 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 tests
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
|
"github.com/google/uuid"
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
historypb "go.temporal.io/api/history/v1"
|
|
workflowpb "go.temporal.io/api/workflow/v1"
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
|
|
enumsspb "go.temporal.io/server/api/enums/v1"
|
|
historyspb "go.temporal.io/server/api/history/v1"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
p "go.temporal.io/server/common/persistence"
|
|
"go.temporal.io/server/common/shuffle"
|
|
"go.temporal.io/server/service/history/tasks"
|
|
)
|
|
|
|
func RandomShardInfo(
|
|
shardID int32,
|
|
rangeID int64,
|
|
) *persistencespb.ShardInfo {
|
|
var shardInfo persistencespb.ShardInfo
|
|
_ = gofakeit.Struct(&shardInfo)
|
|
shardInfo.ShardId = shardID
|
|
shardInfo.RangeId = rangeID
|
|
return &shardInfo
|
|
}
|
|
|
|
func RandomSnapshot(
|
|
namespaceID string,
|
|
workflowID string,
|
|
runID string,
|
|
lastWriteVersion int64,
|
|
state enumsspb.WorkflowExecutionState,
|
|
status enumspb.WorkflowExecutionStatus,
|
|
dbRecordVersion int64,
|
|
) *p.WorkflowSnapshot {
|
|
return &p.WorkflowSnapshot{
|
|
ExecutionInfo: RandomExecutionInfo(namespaceID, workflowID, lastWriteVersion),
|
|
ExecutionState: RandomExecutionState(runID, state, status),
|
|
|
|
NextEventID: rand.Int63(),
|
|
|
|
ActivityInfos: RandomInt64ActivityInfoMap(),
|
|
TimerInfos: RandomStringTimerInfoMap(),
|
|
ChildExecutionInfos: RandomInt64ChildExecutionInfoMap(),
|
|
RequestCancelInfos: RandomInt64RequestCancelInfoMap(),
|
|
SignalInfos: RandomInt64SignalInfoMap(),
|
|
SignalRequestedIDs: map[string]struct{}{uuid.New().String(): {}},
|
|
|
|
Tasks: map[tasks.Category][]tasks.Task{
|
|
tasks.CategoryTransfer: {},
|
|
tasks.CategoryTimer: {},
|
|
tasks.CategoryReplication: {},
|
|
tasks.CategoryVisibility: {},
|
|
},
|
|
|
|
Condition: rand.Int63(),
|
|
DBRecordVersion: dbRecordVersion,
|
|
}
|
|
}
|
|
|
|
func RandomMutation(
|
|
namespaceID string,
|
|
workflowID string,
|
|
runID string,
|
|
lastWriteVersion int64,
|
|
state enumsspb.WorkflowExecutionState,
|
|
status enumspb.WorkflowExecutionStatus,
|
|
dbRecordVersion int64,
|
|
) *p.WorkflowMutation {
|
|
mutation := &p.WorkflowMutation{
|
|
ExecutionInfo: RandomExecutionInfo(namespaceID, workflowID, lastWriteVersion),
|
|
ExecutionState: RandomExecutionState(runID, state, status),
|
|
|
|
NextEventID: rand.Int63(),
|
|
|
|
UpsertActivityInfos: RandomInt64ActivityInfoMap(),
|
|
DeleteActivityInfos: map[int64]struct{}{rand.Int63(): {}},
|
|
UpsertTimerInfos: RandomStringTimerInfoMap(),
|
|
DeleteTimerInfos: map[string]struct{}{uuid.New().String(): {}},
|
|
UpsertChildExecutionInfos: RandomInt64ChildExecutionInfoMap(),
|
|
DeleteChildExecutionInfos: map[int64]struct{}{rand.Int63(): {}},
|
|
UpsertRequestCancelInfos: RandomInt64RequestCancelInfoMap(),
|
|
DeleteRequestCancelInfos: map[int64]struct{}{rand.Int63(): {}},
|
|
UpsertSignalInfos: RandomInt64SignalInfoMap(),
|
|
DeleteSignalInfos: map[int64]struct{}{rand.Int63(): {}},
|
|
UpsertSignalRequestedIDs: map[string]struct{}{uuid.New().String(): {}},
|
|
DeleteSignalRequestedIDs: map[string]struct{}{uuid.New().String(): {}},
|
|
// NewBufferedEvents: see below
|
|
// ClearBufferedEvents: see below
|
|
|
|
Tasks: map[tasks.Category][]tasks.Task{
|
|
tasks.CategoryTransfer: {},
|
|
tasks.CategoryTimer: {},
|
|
tasks.CategoryReplication: {},
|
|
tasks.CategoryVisibility: {},
|
|
},
|
|
|
|
Condition: rand.Int63(),
|
|
DBRecordVersion: dbRecordVersion,
|
|
}
|
|
|
|
switch rand.Int63() % 3 {
|
|
case 0:
|
|
mutation.ClearBufferedEvents = true
|
|
mutation.NewBufferedEvents = nil
|
|
case 1:
|
|
mutation.ClearBufferedEvents = false
|
|
mutation.NewBufferedEvents = nil
|
|
case 2:
|
|
mutation.ClearBufferedEvents = false
|
|
mutation.NewBufferedEvents = []*historypb.HistoryEvent{RandomHistoryEvent()}
|
|
default:
|
|
panic("broken test")
|
|
}
|
|
return mutation
|
|
}
|
|
|
|
func RandomExecutionInfo(
|
|
namespaceID string,
|
|
workflowID string,
|
|
lastWriteVersion int64,
|
|
) *persistencespb.WorkflowExecutionInfo {
|
|
var executionInfo persistencespb.WorkflowExecutionInfo
|
|
_ = gofakeit.Struct(&executionInfo)
|
|
executionInfo.NamespaceId = namespaceID
|
|
executionInfo.WorkflowId = workflowID
|
|
executionInfo.VersionHistories = RandomVersionHistory(lastWriteVersion)
|
|
return &executionInfo
|
|
}
|
|
|
|
func RandomExecutionState(
|
|
runID string,
|
|
state enumsspb.WorkflowExecutionState,
|
|
status enumspb.WorkflowExecutionStatus,
|
|
) *persistencespb.WorkflowExecutionState {
|
|
return &persistencespb.WorkflowExecutionState{
|
|
CreateRequestId: uuid.New().String(),
|
|
RunId: runID,
|
|
State: state,
|
|
Status: status,
|
|
}
|
|
}
|
|
|
|
func RandomInt64ActivityInfoMap() map[int64]*persistencespb.ActivityInfo {
|
|
return map[int64]*persistencespb.ActivityInfo{
|
|
rand.Int63(): RandomActivityInfo(),
|
|
}
|
|
}
|
|
|
|
func RandomStringTimerInfoMap() map[string]*persistencespb.TimerInfo {
|
|
return map[string]*persistencespb.TimerInfo{
|
|
uuid.New().String(): RandomTimerInfo(),
|
|
}
|
|
}
|
|
|
|
func RandomInt64ChildExecutionInfoMap() map[int64]*persistencespb.ChildExecutionInfo {
|
|
return map[int64]*persistencespb.ChildExecutionInfo{
|
|
rand.Int63(): RandomChildExecutionInfo(),
|
|
}
|
|
}
|
|
|
|
func RandomInt64RequestCancelInfoMap() map[int64]*persistencespb.RequestCancelInfo {
|
|
return map[int64]*persistencespb.RequestCancelInfo{
|
|
rand.Int63(): RandomRequestCancelInfo(),
|
|
}
|
|
}
|
|
|
|
func RandomInt64SignalInfoMap() map[int64]*persistencespb.SignalInfo {
|
|
return map[int64]*persistencespb.SignalInfo{
|
|
rand.Int63(): RandomSignalInfo(),
|
|
}
|
|
}
|
|
|
|
func RandomActivityInfo() *persistencespb.ActivityInfo {
|
|
// cannot use gofakeit due to RetryLastFailure is of type Failure
|
|
// and Failure can contain another Failure -> stack overflow
|
|
return &persistencespb.ActivityInfo{
|
|
Version: rand.Int63(),
|
|
ScheduledEventBatchId: rand.Int63(),
|
|
ScheduledTime: RandomTime(),
|
|
StartedEventId: rand.Int63(),
|
|
StartedTime: RandomTime(),
|
|
ActivityId: uuid.New().String(),
|
|
RequestId: uuid.New().String(),
|
|
ScheduleToStartTimeout: RandomDuration(),
|
|
ScheduleToCloseTimeout: RandomDuration(),
|
|
StartToCloseTimeout: RandomDuration(),
|
|
HeartbeatTimeout: RandomDuration(),
|
|
|
|
// other fields omitted, above should be enough for tests
|
|
}
|
|
}
|
|
|
|
func RandomTimerInfo() *persistencespb.TimerInfo {
|
|
var timerInfo persistencespb.TimerInfo
|
|
_ = gofakeit.Struct(&timerInfo)
|
|
return &timerInfo
|
|
}
|
|
|
|
func RandomChildExecutionInfo() *persistencespb.ChildExecutionInfo {
|
|
var childExecutionInfo persistencespb.ChildExecutionInfo
|
|
_ = gofakeit.Struct(&childExecutionInfo)
|
|
return &childExecutionInfo
|
|
}
|
|
|
|
func RandomRequestCancelInfo() *persistencespb.RequestCancelInfo {
|
|
var requestCancelInfo persistencespb.RequestCancelInfo
|
|
_ = gofakeit.Struct(&requestCancelInfo)
|
|
return &requestCancelInfo
|
|
}
|
|
|
|
func RandomSignalInfo() *persistencespb.SignalInfo {
|
|
var signalInfo persistencespb.SignalInfo
|
|
_ = gofakeit.Struct(&signalInfo)
|
|
return &signalInfo
|
|
}
|
|
|
|
func RandomHistoryEvent() *historypb.HistoryEvent {
|
|
var historyEvent historypb.HistoryEvent
|
|
_ = gofakeit.Struct(&historyEvent)
|
|
return &historyEvent
|
|
}
|
|
|
|
func RandomResetPoints() *workflowpb.ResetPoints {
|
|
return &workflowpb.ResetPoints{Points: []*workflowpb.ResetPointInfo{{
|
|
BinaryChecksum: uuid.New().String(),
|
|
RunId: uuid.New().String(),
|
|
FirstWorkflowTaskCompletedId: rand.Int63(),
|
|
CreateTime: RandomTime(),
|
|
ExpireTime: RandomTime(),
|
|
Resettable: rand.Int31()%2 == 0,
|
|
}}}
|
|
}
|
|
|
|
func RandomStringPayloadMap() map[string]*commonpb.Payload {
|
|
return map[string]*commonpb.Payload{
|
|
uuid.New().String(): RandomPayload(),
|
|
}
|
|
}
|
|
|
|
func RandomPayload() *commonpb.Payload {
|
|
var payload commonpb.Payload
|
|
_ = gofakeit.Struct(&payload)
|
|
return &payload
|
|
}
|
|
|
|
func RandomVersionHistory(
|
|
lastWriteVersion int64,
|
|
) *historyspb.VersionHistories {
|
|
return &historyspb.VersionHistories{
|
|
CurrentVersionHistoryIndex: 0,
|
|
Histories: []*historyspb.VersionHistory{{
|
|
BranchToken: shuffle.Bytes([]byte("random branch token")),
|
|
Items: []*historyspb.VersionHistoryItem{{
|
|
EventId: rand.Int63(),
|
|
Version: lastWriteVersion,
|
|
}},
|
|
}},
|
|
}
|
|
}
|
|
|
|
func RandomTime() *timestamppb.Timestamp {
|
|
return timestamppb.New(time.Unix(0, rand.Int63()))
|
|
}
|
|
|
|
func RandomDuration() *durationpb.Duration {
|
|
return durationpb.New(time.Duration(rand.Int63()))
|
|
}
|