123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package test
- import (
- "errors"
- "fmt"
- "git.sxidc.com/service-supports/dapr_api/saga"
- "git.sxidc.com/service-supports/dapr_api/state"
- "testing"
- "time"
- )
- func TestSagaOK(t *testing.T) {
- stateAPI := state.NewAPI(daprHttpPort, 10*time.Second)
- defer state.DestroyAPI(stateAPI)
- saga.Init(stateAPI, stateStoreName, "sage_test")
- defer saga.Destroy()
- orchestrator := saga.GetInstance().BuildOrchestrator("test-ok", 5*time.Second).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("one")
- return nil
- },
- StepRollbackFunc: func() error {
- fmt.Println("one rollback")
- return nil
- },
- }).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("two")
- return nil
- },
- StepRollbackFunc: func() error {
- fmt.Println("two rollback")
- return nil
- },
- }).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("three")
- return nil
- },
- StepRollbackFunc: nil,
- }).
- Output(func() (interface{}, error) {
- return "ok result", nil
- })
- result, err := orchestrator.Run()
- if err != nil {
- t.Fatal(err)
- }
- fmt.Println("Result:", result)
- }
- func TestSagaRollback(t *testing.T) {
- stateAPI := state.NewAPI(daprHttpPort, 10*time.Second)
- defer state.DestroyAPI(stateAPI)
- saga.Init(stateAPI, stateStoreName, "sage_test")
- defer saga.Destroy()
- orchestrator := saga.GetInstance().BuildOrchestrator("test-rollback", 5*time.Second).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("one")
- return nil
- },
- StepRollbackFunc: func() error {
- fmt.Println("one rollback")
- return nil
- },
- }).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("two")
- return nil
- },
- StepRollbackFunc: func() error {
- fmt.Println("two rollback")
- return nil
- },
- }).
- AddStep(&saga.Step{
- StepFunc: func() error {
- fmt.Println("three")
- return nil
- },
- StepRollbackFunc: nil,
- }).
- AddStep(&saga.Step{
- StepFunc: func() error {
- return errors.New("four error")
- },
- StepRollbackFunc: nil,
- }).
- Output(func() (interface{}, error) {
- return "should not come here", nil
- })
- result, err := orchestrator.Run()
- if err != nil {
- t.Fatal(err)
- }
- fmt.Println("Result:", result)
- time.Sleep(1 * time.Second)
- }
|