Skip to content

Commit

Permalink
Update 3DC integration test (cadence-workflow#2503)
Browse files Browse the repository at this point in the history
* merge attribute generation into event generator

* add randomize version function
  • Loading branch information
yux0 authored Sep 5, 2019
1 parent ce0b4d4 commit 06ecc10
Show file tree
Hide file tree
Showing 5 changed files with 899 additions and 799 deletions.
100 changes: 88 additions & 12 deletions common/testing/event_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package testing
import (
"math/rand"
"strings"
"time"
)

const (
emptyCandidateIndex = -1
versionBumpGap = int64(100)
)

var (
Expand All @@ -42,6 +42,8 @@ type (
name string
isStrictOnNextVertex bool
maxNextGeneration int
dataFunc func(...interface{}) interface{}
data interface{}
}

// HistoryEventModel is a graph represents relationships among history event types
Expand All @@ -60,8 +62,10 @@ type (
exitVertices map[Vertex]bool
randomEntryVertices []Vertex
dice *rand.Rand
seed int64
canDoBatch func([]Vertex) bool
resetPoints []ResetPoint
resetCount int64
}

// ResetPoint is a mark in the generated event history that generator can be reset to
Expand Down Expand Up @@ -90,6 +94,7 @@ type (
func NewEventGenerator(
seed int64,
) Generator {

return &EventGenerator{
connections: make(map[Vertex][]Edge),
previousVertices: make([]Vertex, 0),
Expand All @@ -98,8 +103,10 @@ func NewEventGenerator(
exitVertices: make(map[Vertex]bool),
randomEntryVertices: make([]Vertex, 0),
dice: rand.New(rand.NewSource(seed)),
seed: seed,
canDoBatch: defaultBatchFunc,
resetPoints: make([]ResetPoint, 0),
resetCount: 0,
}
}

Expand Down Expand Up @@ -184,7 +191,6 @@ func (g *EventGenerator) Reset() {

g.leafVertices = make([]Vertex, 0)
g.previousVertices = make([]Vertex, 0)
g.dice = rand.New(rand.NewSource(time.Now().Unix()))
}

// ListResetPoint returns a list of available point to reset the event generator
Expand All @@ -195,27 +201,45 @@ func (g *EventGenerator) ListResetPoint() []ResetPoint {
}

// RandomResetToResetPoint randomly pick a reset point and reset the event generator to the point
func (g *EventGenerator) RandomResetToResetPoint() int {
func (g *EventGenerator) RandomResetToResetPoint() Generator {

// Random reset does not reset to index 0
nextIdx := g.dice.Intn(len(g.resetPoints)-1) + 1
g.ResetToResetPoint(nextIdx)
return nextIdx
return g.ResetToResetPoint(nextIdx)
}

// ResetToResetPoint resets to the corresponding reset point based on the input reset point index
func (g *EventGenerator) ResetToResetPoint(
index int,
) {
) Generator {

if index >= len(g.resetPoints) {
panic("The reset point does not exist.")
}
toReset := g.resetPoints[index]
g.previousVertices = toReset.previousVertices
g.leafVertices = toReset.leafVertices
g.resetPoints = g.resetPoints[index:]
g.dice = rand.New(rand.NewSource(time.Now().Unix()))
previousVertices := make([]Vertex, len(toReset.previousVertices))
copy(previousVertices, toReset.previousVertices)
leafVertices := make([]Vertex, len(toReset.leafVertices))
copy(leafVertices, toReset.leafVertices)
entryVertices := make([]Vertex, len(g.entryVertices))
copy(entryVertices, g.entryVertices)
randomEntryVertices := make([]Vertex, len(g.randomEntryVertices))
copy(randomEntryVertices, g.randomEntryVertices)
resetPoints := make([]ResetPoint, len(g.resetPoints[index:]))
copy(resetPoints, g.resetPoints[index:])
return &EventGenerator{
connections: copyConnections(g.connections),
previousVertices: previousVertices,
leafVertices: leafVertices,
entryVertices: entryVertices,
exitVertices: copyExitVertices(g.exitVertices),
randomEntryVertices: randomEntryVertices,
dice: rand.New(rand.NewSource(g.seed)),
seed: g.seed,
canDoBatch: g.canDoBatch,
resetPoints: resetPoints,
resetCount: g.resetCount + 1,
}
}

// SetBatchGenerationRule sets a function to determine next generated batch of history events
Expand Down Expand Up @@ -274,6 +298,7 @@ func (g *EventGenerator) getEntryVertex() Vertex {
nextRange := len(g.entryVertices)
nextIdx := g.dice.Intn(nextRange)
vertex := g.entryVertices[nextIdx]
vertex.GenerateData(nil)
return vertex
}

Expand All @@ -285,6 +310,13 @@ func (g *EventGenerator) getRandomVertex() Vertex {
nextRange := len(g.randomEntryVertices)
nextIdx := g.dice.Intn(nextRange)
vertex := g.randomEntryVertices[nextIdx]
lastEvent := g.previousVertices[len(g.previousVertices)-1]

versionBump := int64(0)
if g.shouldBumpVersion() {
versionBump = versionBumpGap
}
vertex.GenerateData(lastEvent.GetData(), int64(1), versionBump, g.resetCount)
return vertex
}

Expand Down Expand Up @@ -335,10 +367,16 @@ func (g *EventGenerator) randomNextVertex(
) []Vertex {

nextVertex := g.leafVertices[nextVertexIdx]
count := g.dice.Intn(nextVertex.GetMaxNextVertex()) + 1
versionBump := int64(0)
if g.shouldBumpVersion() {
versionBump = versionBumpGap
}

count := g.dice.Intn(nextVertex.GetMaxNextVertex()) + 2
res := make([]Vertex, 0)
for i := 0; i < count; i++ {
for i := 1; i < count; i++ {
endVertex := g.pickRandomVertex(nextVertex)
endVertex.GenerateData(nextVertex.GetData(), int64(i), versionBump, g.resetCount)
res = append(res, endVertex)
if _, ok := g.exitVertices[endVertex]; ok {
res = []Vertex{endVertex}
Expand Down Expand Up @@ -366,6 +404,11 @@ func (g *EventGenerator) pickRandomVertex(
return endVertex
}

func (g *EventGenerator) shouldBumpVersion() bool {
// 1//1000 to bump the version
return g.dice.Intn(1000) == 500
}

// NewHistoryEventEdge initials a new edge between two HistoryEventVertexes
func NewHistoryEventEdge(
start Vertex,
Expand Down Expand Up @@ -495,6 +538,39 @@ func (he HistoryEventVertex) GetMaxNextVertex() int {
return he.maxNextGeneration
}

// SetDataFunc sets the data generation function
func (he *HistoryEventVertex) SetDataFunc(
dataFunc func(...interface{}) interface{},
) {

he.dataFunc = dataFunc
}

// GetDataFunc returns the data generation function
func (he HistoryEventVertex) GetDataFunc() func(...interface{}) interface{} {

return he.dataFunc
}

// GenerateData generates the data and return
func (he *HistoryEventVertex) GenerateData(
input ...interface{},
) interface{} {

if he.dataFunc == nil {
return nil
}

he.data = he.dataFunc(input...)
return he.data
}

// GetData returns the vertex data
func (he HistoryEventVertex) GetData() interface{} {

return he.data
}

// NewHistoryEventModel initials new history event model
func NewHistoryEventModel() Model {

Expand Down
10 changes: 8 additions & 2 deletions common/testing/generator_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ type (
// ListResetPoint lists all available reset points
ListResetPoint() []ResetPoint
// RandomResetToResetPoint randomly pick a reset point to reset and return the reset point index
RandomResetToResetPoint() int
RandomResetToResetPoint() Generator
// ResetToResetPoint resets the generator to a reset point
ResetToResetPoint(int)
ResetToResetPoint(int) Generator
// SetBatchGenerationRule sets a function that used in GetNextVertex to return batch result
SetBatchGenerationRule(func([]Vertex) bool)
}
Expand All @@ -75,6 +75,12 @@ type (
// MaxNextVertex means the max neighbors can branch out from this vertex
SetMaxNextVertex(int)
GetMaxNextVertex() int

// SetVertexDataFunc sets a function to generate end vertex data
SetDataFunc(func(...interface{}) interface{})
GetDataFunc() func(...interface{}) interface{}
GenerateData(...interface{}) interface{}
GetData() interface{}
}

// Edge is the connection between two vertices
Expand Down
64 changes: 15 additions & 49 deletions common/testing/history_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"fmt"
"testing"

"github.com/pborman/uuid"
"github.com/stretchr/testify/suite"
"github.com/uber/cadence/.gen/go/shared"
)

type (
Expand All @@ -40,7 +40,7 @@ func TestHistoryEventTestSuite(t *testing.T) {
}

func (s *historyEventTestSuit) SetupSuite() {
s.generator = InitializeHistoryEventGenerator()
s.generator = InitializeHistoryEventGenerator("domain")
}

func (s *historyEventTestSuit) SetupTest() {
Expand All @@ -49,56 +49,22 @@ func (s *historyEventTestSuit) SetupTest() {

// This is a sample about how to use the generator
func (s *historyEventTestSuit) Test_HistoryEvent_Generator() {

totalBranchNumber := 2
currentBranch := totalBranchNumber
root := &NDCTestBranch{
Batches: make([]NDCTestBatch, 0),
}
curr := root
// eventRanches := make([][]Vertex, 0, totalBranchNumber)
for currentBranch > 0 {
for s.generator.HasNextVertex() {
events := s.generator.GetNextVertices()
newBatch := NDCTestBatch{
Events: events,
}
curr.Batches = append(curr.Batches, newBatch)
for _, e := range events {
fmt.Println(e.GetName())
}
}
currentBranch--
if currentBranch > 0 {
resetIdx := s.generator.RandomResetToResetPoint()
curr = root.Split(resetIdx)
for s.generator.HasNextVertex() {
events := s.generator.GetNextVertices()
for _, e := range events {
fmt.Println(e.GetName())
fmt.Println(e.GetData().(*shared.HistoryEvent).GetEventId())
}
}
s.NotEmpty(s.generator.ListGeneratedVertices())
queue := []*NDCTestBranch{root}
for len(queue) > 0 {
b := queue[0]
queue = queue[1:]
for _, batch := range b.Batches {
for _, event := range batch.Events {
fmt.Println(event.GetName())
}

newGenerator := s.generator.RandomResetToResetPoint()
for newGenerator.HasNextVertex() {
events := newGenerator.GetNextVertices()
for _, e := range events {
fmt.Println(e.GetName())
fmt.Println(e.GetData().(*shared.HistoryEvent).GetEventId())
}
queue = append(queue, b.Next...)
}

// Generator one branch of history events
batches := []NDCTestBatch{}
batches = append(batches, root.Batches...)
batches = append(batches, root.Next[0].Batches...)
identity := "test-event-generator"
wid := uuid.New()
rid := uuid.New()
wt := "event-generator-workflow-type"
tl := "event-generator-taskList"
domain := "event-generator"
domainID := uuid.New()
attributeGenerator := NewHistoryAttributesGenerator(wid, rid, tl, wt, domain, domainID, identity)
history := attributeGenerator.GenerateHistoryEvents(batches, 1, 100)
s.NotEmpty(history)
s.NotEmpty(newGenerator.ListGeneratedVertices())
}
Loading

0 comments on commit 06ecc10

Please sign in to comment.