Skip to content

Commit c0370e1

Browse files
authored
added QueryTypeQueryTypes and unit tests (#1295)
1 parent c20b993 commit c0370e1

File tree

7 files changed

+99
-5
lines changed

7 files changed

+99
-5
lines changed

client/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const (
4343
// QueryTypeOpenSessions is the build in query type for Client.QueryWorkflow() call. Use this query type to get all open
4444
// sessions in the workflow. The result will be a list of SessionInfo encoded in the encoded.Value.
4545
QueryTypeOpenSessions string = internal.QueryTypeOpenSessions
46+
47+
// QueryTypeQueryTypes is the build in query type for Client.QueryWorkflow() call. Use this query type to list
48+
// all query types of the workflow. The result will be a string encoded in the EncodedValue.
49+
QueryTypeQueryTypes string = internal.QueryTypeQueryTypes
4650
)
4751

4852
type (

internal/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,21 @@ const (
4545
// QueryTypeOpenSessions is the build in query type for Client.QueryWorkflow() call. Use this query type to get all open
4646
// sessions in the workflow. The result will be a list of SessionInfo encoded in the EncodedValue.
4747
QueryTypeOpenSessions string = "__open_sessions"
48+
49+
// QueryTypeQueryTypes is the build in query type for Client.QueryWorkflow() call. Use this query type to list
50+
// all query types of the workflow. The result will be a string encoded in the EncodedValue.
51+
QueryTypeQueryTypes string = "__query_types"
4852
)
4953

54+
// BuiltinQueryTypes returns a list of built-in query types
55+
func BuiltinQueryTypes() []string {
56+
return []string{
57+
QueryTypeOpenSessions,
58+
QueryTypeStackTrace,
59+
QueryTypeQueryTypes,
60+
}
61+
}
62+
5063
type Option interface{ private() }
5164

5265
type CancelReason string

internal/internal_event_handlers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ func (weh *workflowExecutionEventHandlerImpl) ProcessQuery(queryType string, que
961961
return weh.encodeArg(weh.StackTrace())
962962
case QueryTypeOpenSessions:
963963
return weh.encodeArg(weh.getOpenSessions())
964+
case QueryTypeQueryTypes:
965+
return weh.encodeArg(weh.KnownQueryTypes())
964966
default:
965967
result, err := weh.queryHandler(queryType, queryArgs)
966968
if err != nil {
@@ -980,6 +982,10 @@ func (weh *workflowExecutionEventHandlerImpl) ProcessQuery(queryType string, que
980982
}
981983
}
982984

985+
func (weh *workflowExecutionEventHandlerImpl) KnownQueryTypes() []string {
986+
return weh.workflowDefinition.KnownQueryTypes()
987+
}
988+
983989
func (weh *workflowExecutionEventHandlerImpl) StackTrace() string {
984990
return weh.workflowDefinition.StackTrace()
985991
}

internal/internal_event_handlers_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,22 @@ func TestHistoryEstimationforPackedEvents(t *testing.T) {
382382
trueSize := len(testEvents)*historySizeEstimationBuffer + len(byteArray)*2*len(testEvents)
383383
assert.Equal(t, trueSize, historySizeSum)
384384
}
385+
386+
func TestProcessQuery_KnownQueryTypes(t *testing.T) {
387+
rootCtx := setWorkflowEnvOptionsIfNotExist(Background())
388+
eo := getWorkflowEnvOptions(rootCtx)
389+
eo.queryHandlers["a"] = nil
390+
391+
weh := &workflowExecutionEventHandlerImpl{
392+
workflowEnvironmentImpl: &workflowEnvironmentImpl{
393+
dataConverter: DefaultDataConverter,
394+
},
395+
workflowDefinition: &syncWorkflowDefinition{
396+
rootCtx: rootCtx,
397+
},
398+
}
399+
400+
result, err := weh.ProcessQuery(QueryTypeQueryTypes, nil)
401+
assert.NoError(t, err)
402+
assert.Equal(t, "[\"__open_sessions\",\"__query_types\",\"__stack_trace\",\"a\"]\n", string(result))
403+
}

internal/internal_worker_base.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ type (
108108
// Executed after all history events since the previous decision are applied to workflowDefinition
109109
OnDecisionTaskStarted()
110110
StackTrace() string // Stack trace of all coroutines owned by the Dispatcher instance
111+
112+
// KnownQueryTypes returns a list of known query types of the workflowOptions with BuiltinQueryTypes
113+
KnownQueryTypes() []string
111114
Close()
112115
}
113116

internal/internal_workflow.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"reflect"
3030
"runtime"
31+
"sort"
3132
"strings"
3233
"sync"
3334
"time"
@@ -501,11 +502,7 @@ func (d *syncWorkflowDefinition) Execute(env workflowEnvironment, header *shared
501502
eo := getWorkflowEnvOptions(d.rootCtx)
502503
handler, ok := eo.queryHandlers[queryType]
503504
if !ok {
504-
keys := []string{QueryTypeStackTrace, QueryTypeOpenSessions}
505-
for k := range eo.queryHandlers {
506-
keys = append(keys, k)
507-
}
508-
return nil, fmt.Errorf("unknown queryType %v. KnownQueryTypes=%v", queryType, keys)
505+
return nil, fmt.Errorf("unknown queryType %v. KnownQueryTypes=%v", queryType, eo.KnownQueryTypes())
509506
}
510507
return handler(queryArgs)
511508
})
@@ -519,6 +516,10 @@ func (d *syncWorkflowDefinition) StackTrace() string {
519516
return d.dispatcher.StackTrace()
520517
}
521518

519+
func (d *syncWorkflowDefinition) KnownQueryTypes() []string {
520+
return getWorkflowEnvOptions(d.rootCtx).KnownQueryTypes()
521+
}
522+
522523
func (d *syncWorkflowDefinition) Close() {
523524
if d.dispatcher != nil {
524525
d.dispatcher.Close()
@@ -1284,6 +1285,18 @@ func (w *workflowOptions) getUnhandledSignalNames() []string {
12841285
return unhandledSignals
12851286
}
12861287

1288+
// KnownQueryTypes returns a list of known query types of the workflowOptions with BuiltinQueryTypes
1289+
func (w *workflowOptions) KnownQueryTypes() []string {
1290+
keys := BuiltinQueryTypes()
1291+
1292+
for k := range w.queryHandlers {
1293+
keys = append(keys, k)
1294+
}
1295+
1296+
sort.Strings(keys)
1297+
return keys
1298+
}
1299+
12871300
func (d *decodeFutureImpl) Get(ctx Context, value interface{}) error {
12881301
more := d.futureImpl.channel.Receive(ctx, nil)
12891302
if more {

internal/internal_workflow_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,3 +1308,39 @@ func (t *tracingInterceptor) ExecuteWorkflow(ctx Context, workflowType string, a
13081308
t.trace = append(t.trace, "ExecuteWorkflow "+workflowType+" end")
13091309
return result
13101310
}
1311+
1312+
type WorkflowOptionTest struct {
1313+
suite.Suite
1314+
}
1315+
1316+
func TestWorkflowOption(t *testing.T) {
1317+
suite.Run(t, new(WorkflowOptionTest))
1318+
}
1319+
1320+
func (t *WorkflowOptionTest) TestKnowQueryType_NoHandlers() {
1321+
wo := workflowOptions{queryHandlers: make(map[string]func([]byte) ([]byte, error))}
1322+
t.ElementsMatch(
1323+
[]string{
1324+
QueryTypeStackTrace,
1325+
QueryTypeOpenSessions,
1326+
QueryTypeQueryTypes,
1327+
},
1328+
wo.KnownQueryTypes())
1329+
}
1330+
1331+
func (t *WorkflowOptionTest) TestKnowQueryType_WithHandlers() {
1332+
wo := workflowOptions{queryHandlers: map[string]func([]byte) ([]byte, error){
1333+
"a": nil,
1334+
"b": nil,
1335+
}}
1336+
1337+
t.ElementsMatch(
1338+
[]string{
1339+
QueryTypeStackTrace,
1340+
QueryTypeOpenSessions,
1341+
QueryTypeQueryTypes,
1342+
"a",
1343+
"b",
1344+
},
1345+
wo.KnownQueryTypes())
1346+
}

0 commit comments

Comments
 (0)