go.temporal.io/server@v1.23.0/common/testing/history_event_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package testing
    26  
    27  import (
    28  	"fmt"
    29  	"runtime/debug"
    30  	"testing"
    31  
    32  	"github.com/stretchr/testify/suite"
    33  	historypb "go.temporal.io/api/history/v1"
    34  )
    35  
    36  type (
    37  	historyEventTestSuit struct {
    38  		suite.Suite
    39  		generator Generator
    40  	}
    41  )
    42  
    43  func TestHistoryEventTestSuite(t *testing.T) {
    44  	suite.Run(t, new(historyEventTestSuit))
    45  }
    46  
    47  func (s *historyEventTestSuit) SetupSuite() {
    48  	s.generator = InitializeHistoryEventGenerator("namespace", "ns-id", 1)
    49  }
    50  
    51  func (s *historyEventTestSuit) SetupTest() {
    52  	s.generator.Reset()
    53  }
    54  
    55  // This is a sample about how to use the generator
    56  func (s *historyEventTestSuit) Test_HistoryEvent_Generator() {
    57  	defer func() {
    58  		if r := recover(); r != nil {
    59  			fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
    60  		}
    61  	}()
    62  	maxEventID := int64(0)
    63  	maxVersion := int64(1)
    64  	maxTaskID := int64(1)
    65  	for i := 0; i < 10 && s.generator.HasNextVertex(); i++ {
    66  		events := s.generator.GetNextVertices()
    67  
    68  		fmt.Println("########################")
    69  		for _, e := range events {
    70  			event := e.GetData().(*historypb.HistoryEvent)
    71  			if maxEventID != event.GetEventId()-1 {
    72  				s.Fail("event id sequence is incorrect")
    73  			}
    74  			maxEventID = event.GetEventId()
    75  			if maxVersion > event.GetVersion() {
    76  				s.Fail("event version is incorrect")
    77  			}
    78  			maxVersion = event.GetVersion()
    79  			if maxTaskID > event.GetTaskId() {
    80  				s.Fail("event task id is incorrect")
    81  			}
    82  			maxTaskID = event.GetTaskId()
    83  			fmt.Println(e.GetName())
    84  			fmt.Println(event.GetEventId())
    85  		}
    86  	}
    87  	s.NotEmpty(s.generator.ListGeneratedVertices())
    88  	fmt.Println("==========================")
    89  	branchGenerator1 := s.generator.DeepCopy()
    90  	for i := 0; i < 10 && branchGenerator1.HasNextVertex(); i++ {
    91  		events := branchGenerator1.GetNextVertices()
    92  		fmt.Println("########################")
    93  		for _, e := range events {
    94  			event := e.GetData().(*historypb.HistoryEvent)
    95  			if maxEventID != event.GetEventId()-1 {
    96  				s.Fail("event id sequence is incorrect")
    97  			}
    98  			maxEventID = event.GetEventId()
    99  			if maxVersion > event.GetVersion() {
   100  				s.Fail("event version is incorrect")
   101  			}
   102  			maxVersion = event.GetVersion()
   103  			if maxTaskID > event.GetTaskId() {
   104  				s.Fail("event task id is incorrect")
   105  			}
   106  			maxTaskID = event.GetTaskId()
   107  			fmt.Println(e.GetName())
   108  			fmt.Println(event.GetEventId())
   109  		}
   110  	}
   111  	fmt.Println("==========================")
   112  	history := s.generator.ListGeneratedVertices()
   113  	maxEventID = history[len(history)-1].GetData().(*historypb.HistoryEvent).GetEventId()
   114  	for i := 0; i < 10 && s.generator.HasNextVertex(); i++ {
   115  		events := s.generator.GetNextVertices()
   116  		fmt.Println("########################")
   117  		for _, e := range events {
   118  			event := e.GetData().(*historypb.HistoryEvent)
   119  			if maxEventID != event.GetEventId()-1 {
   120  				s.Fail("event id sequence is incorrect")
   121  			}
   122  			maxEventID = event.GetEventId()
   123  			if maxVersion > event.GetVersion() {
   124  				s.Fail("event version is incorrect")
   125  			}
   126  			maxVersion = event.GetVersion()
   127  			if maxTaskID > event.GetTaskId() {
   128  				s.Fail("event task id is incorrect")
   129  			}
   130  			maxTaskID = event.GetTaskId()
   131  			fmt.Println(e.GetName())
   132  			fmt.Println(event.GetEventId())
   133  		}
   134  	}
   135  }