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) }