transdsl

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2018 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Transaction DSL

trans-dsl is a transaction model framework in golang, can be used in complex business scenes.

Introduction

In some complex domains, a business process may involve the interaction of multiple messages, which may be synchronous messages, asynchronous messages, or even system calls. We explicitly model the operation behavior of message interaction, and call the operation behavior of a message interaction as Action, then the flow chart of the business corresponds to an Action sequence. In general, a single scenario business process corresponds to a transaction process. Based on the transaction model framework, business developers can not only express all business processes in a simple and complete manner at a higher level, but also apply and maintain transaction models at low cost.

Chinese reader references

Features

  • support synchronous messages
  • support system calls
  • support asynchronous messages

Installation


$ go get github.com/agiledragon/trans-dsl

Keyword

  • optional
  • loop
  • retry
  • repeat
  • wait
  • concurrent
  • procedure
  • succ
  • fail
  • not
  • allof
  • anyof

Using Transaction DSL

Firstly, let's look at a transaction example.

During the synchronization request processing from S1 to S2, the perspective of standing at S1 is an Action, while the perspective of standing at S2 is a transaction.

func newS1ReqTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			new(action.Action1),
			&transdsl.Optional{
				Spec:   new(spec.ShouldExecAction2),
				IfFrag: new(action.Action2),
			},
			&transdsl.Loop{
				FuncVar: newProcedure1,
			},
			new(action.Action3),
		},
	}
	return trans
}

func newProcedure1() transdsl.Fragment {
	procedure := &transdsl.Procedure{
		Fragments: []transdsl.Fragment{
			new(action.Action11),
			&transdsl.Optional{
				Spec:   new(spec.ShouldExecProcedure2),
				IfFrag: newProcedure2(),
			},
			new(action.Action12),
		},
	}
	return procedure
}

func newProcedure2() transdsl.Fragment {
	procedure := &transdsl.Procedure{
		Fragments: []transdsl.Fragment{
			new(action.Action21),
			new(action.Action22),
		},
	}
	return procedure
}


The following just make some tests as typical examples. Please refer to the test cases, very complete and detailed.

optional
import (
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	"github.com/agiledragon/trans-dsl/test/context/spec"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func newIfTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			&transdsl.Optional{
				Spec:   new(spec.IsAbcExist),
				IfFrag: new(action.StubConnectAbc),
			},
			new(action.StubActivateSomething),
		},
	}
	return trans
}

func newElseTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			&transdsl.Optional{
				Spec:     new(spec.IsAbcExist),
				IfFrag:   new(action.StubConnectAbc),
				ElseFrag: new(action.StubConnectDef),
			},
			new(action.StubActivateSomething),
		},
	}
	return trans
}

func TestIfTrans(t *testing.T) {
	trans := newIfTrans()
	Convey("TestIfTrans", t, func() {

		Convey("trans exec succ when spec is true", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					Abc: "abc",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 2)
		})

		Convey("trans exec succ when spec is false", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					Abc: "def",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 1)
		})

		Convey("iffrag rollback", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					X:   "test",
					Abc: "abc",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldNotEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 0)
		})
	})
}

func TestElseTrans(t *testing.T) {
	trans := newElseTrans()
	Convey("TestElseTrans", t, func() {

		Convey("trans exec succ when spec is false", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					Abc: "def",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 3)
		})

		Convey("elsefrag rollback", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					X:   "test",
					Abc: "def",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldNotEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, -1)
		})
	})
}

loop
import (
	"errors"
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func newLoopTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			new(action.StubGetSomething),
			&transdsl.Loop{
				FuncVar:      newLoopProcedure,
				BreakErrs:    []error{errors.New("break1"), errors.New("break2")},
				ContinueErrs: []error{errors.New("continue1"), errors.New("continue2")},
			},
		},
	}
	return trans
}

func newLoopProcedure() transdsl.Fragment {
	procedure := &transdsl.Procedure{
		Fragments: []transdsl.Fragment{
			new(action.StubAttachSomething),
			new(action.StubActivateSomething),
		},
	}
	return procedure
}

func TestLoopTrans(t *testing.T) {
	trans := newLoopTrans()
	Convey("TestLoopTrans", t, func() {

		Convey("trans exec succ when loop 3 times", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					LoopValue: 1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).LoopValue, ShouldEqual, 4)
		})

		Convey("trans exec succ when second time break", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					LoopValue: 1,
					Abc:       "break",
				},
			}
			err := trans.Start(transInfo)
			So(err.Error(), ShouldEqual, "break2")
			So(transInfo.AppInfo.(*context.StubInfo).LoopValue, ShouldEqual, 2)

		})

		Convey("trans exec succ when third time continue", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					LoopValue: 1,
					Abc:       "continue",
				},
			}
			err := trans.Start(transInfo)
			So(err.Error(), ShouldEqual, "continue2")
			So(transInfo.AppInfo.(*context.StubInfo).LoopValue, ShouldEqual, 3)

		})

		Convey("trans exec fail", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					LoopValue: 1,
					X:         "test",
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldNotEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).LoopValue, ShouldEqual, 1)
		})
	})
}


retry
import (
	"errors"
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func newRetryTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			&transdsl.Retry{
				MaxTimes: 3,
				TimeLen:  100,
				Fragment: new(action.StubConnectServer),
				Errs:     []error{errors.New("fatal"), errors.New("panic")},
			},
		},
	}
	return trans
}

func TestRetryTrans(t *testing.T) {
	trans := newRetryTrans()
	Convey("TestRetryTrans", t, func() {

		Convey("trans exec succ when fail time is 1", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					FailTimes: 1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
		})

		Convey("trans exec succ when fail time is 2", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					FailTimes: 2,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
		})

		Convey("trans exec fail when fail time is 3", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					FailTimes: 3,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldNotEqual, nil)
		})

		Convey("trans exec fail when error string is panic", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					FailTimes: 1,
					Y:         -1,
				},
			}
			err := trans.Start(transInfo)
			So(err.Error(), ShouldEqual, "panic")
		})
	})
}

wait
import (
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
	"time"
)

var eventId = "assign cmd"

func newWaitTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			new(action.StubTransferMoney),
			&transdsl.Wait{
				EventId:  eventId,
				Timeout:  100,
				Fragment: new(action.StubAssignCmd),
			},
			new(action.StubAttachSomething),
			new(action.StubActivateSomething),
		},
	}
	return trans
}

type TransObj struct {
	trans     *transdsl.Transaction
	transInfo *transdsl.TransInfo
}

var transIds map[string]string
var transObjs map[string]TransObj
var key string

func handleEvent(eventId string, eventContent []byte) {
	transId := transIds[key]
	transObj := transObjs[transId]
	trans := transObj.trans
	transInfo := transObj.transInfo
	stubInfo := transInfo.AppInfo.(*context.StubInfo)
	stubInfo.EventContent = eventContent
	<-time.After(50 * time.Millisecond)
	trans.HandleEvent(eventId, transInfo)
}

func TestWaitTrans(t *testing.T) {
	trans := newWaitTrans()
	transIds = make(map[string]string)
	key = "business id"
	transId := "123456"
	transIds[key] = transId

	transObjs = make(map[string]TransObj)
	transInfo := &transdsl.TransInfo{
		Ch: make(chan struct{}),
		AppInfo: &context.StubInfo{
			TransId: "",
			X:       "info",
			Y:       1,
		},
	}
	transObjs[transId] = TransObj{trans: trans, transInfo: transInfo}
	Convey("TestWaitTrans", t, func() {

		Convey("wait succ", func() {
			go handleEvent(eventId, nil)
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 8)
		})

		Convey("wait timeout", func() {
			transInfo := &transdsl.TransInfo{
				Ch: make(chan struct{}),
				AppInfo: &context.StubInfo{
					X: "info",
					Y: 1,
				},
			}
			err := trans.Start(transInfo)
			So(err.Error(), ShouldEqual, transdsl.ErrTimeout.Error())
		})
	})
}


allof
import (
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	"github.com/agiledragon/trans-dsl/test/context/spec"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func newAllOfTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			&transdsl.Optional{
				Spec: &transdsl.AllOf{
					Specs: []transdsl.Specification{
						new(spec.IsAbcExist),
						new(spec.IsDefExist),
						new(spec.IsGhiExist),
					},
				},
				IfFrag: new(action.StubConnectAbc),
			},
			new(action.StubActivateSomething),
		},
	}
	return trans
}

func TestAllOfTrans(t *testing.T) {
	trans := newAllOfTrans()
	Convey("TestAllOfTrans", t, func() {

		Convey("all specs are true", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					Abc: "abc",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 2)
		})

		Convey("one of specs is false", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					Abc: "def",
					Y:   1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).Y, ShouldEqual, 1)
		})
	})
}

fail
import (
	"errors"
	"github.com/agiledragon/trans-dsl"
	"github.com/agiledragon/trans-dsl/test/context"
	"github.com/agiledragon/trans-dsl/test/context/action"
	"github.com/agiledragon/trans-dsl/test/context/spec"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

var ErrResourceInsufficient = errors.New("resource insufficient")

func newFailTrans() *transdsl.Transaction {
	trans := &transdsl.Transaction{
		Fragments: []transdsl.Fragment{
			new(action.StubAttachSomething),
			&transdsl.Optional{
				Spec: new(spec.IsSomeResourceInsufficient),
				IfFrag: &transdsl.Fail{
					ErrCode: ErrResourceInsufficient,
				},
			},
			new(action.StubActivateSomething),
		},
	}
	return trans
}

func TestFailTrans(t *testing.T) {
	trans := newFailTrans()
	Convey("TestFailTrans", t, func() {

		Convey("spec is true", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					X:          "insufficient",
					SpecialNum: 1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, ErrResourceInsufficient)
			So(transInfo.AppInfo.(*context.StubInfo).SpecialNum, ShouldEqual, 10)
		})

		Convey("spec is false", func() {
			transInfo := &transdsl.TransInfo{
				AppInfo: &context.StubInfo{
					X:          "sufficient",
					SpecialNum: 1,
				},
			}
			err := trans.Start(transInfo)
			So(err, ShouldEqual, nil)
			So(transInfo.AppInfo.(*context.StubInfo).SpecialNum, ShouldEqual, 20)
		})
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSucc            = errors.New("trans succ")
	ErrUnexpectedEvent = errors.New("unexpected event")
	ErrTimeout         = errors.New("timeout")
)

Functions

This section is empty.

Types

type AllOf

type AllOf struct {
	Specs []Specification
}

func (*AllOf) Ok

func (this *AllOf) Ok(transInfo *TransInfo) bool

type AnyOf

type AnyOf struct {
	Specs []Specification
}

func (*AnyOf) Ok

func (this *AnyOf) Ok(transInfo *TransInfo) bool

type Concurrent

type Concurrent struct {
	Fragments []Fragment
}

func (*Concurrent) Exec

func (this *Concurrent) Exec(transInfo *TransInfo) error

func (*Concurrent) Rollback

func (this *Concurrent) Rollback(transInfo *TransInfo)

type Fail

type Fail struct {
	ErrCode error
}

func (*Fail) Exec

func (this *Fail) Exec(transInfo *TransInfo) error

func (*Fail) Rollback

func (this *Fail) Rollback(transInfo *TransInfo)

type Fragment

type Fragment interface {
	Exec(transInfo *TransInfo) error
	Rollback(transInfo *TransInfo)
}

type Loop

type Loop struct {
	FuncVar      func() Fragment
	BreakErrs    []error
	ContinueErrs []error
	// contains filtered or unexported fields
}

func (*Loop) Exec

func (this *Loop) Exec(transInfo *TransInfo) error

func (*Loop) Rollback

func (this *Loop) Rollback(transInfo *TransInfo)

type Not

type Not struct {
	Spec Specification
}

func (*Not) Ok

func (this *Not) Ok(transInfo *TransInfo) bool

type Optional

type Optional struct {
	Spec     Specification
	IfFrag   Fragment
	ElseFrag Fragment
	// contains filtered or unexported fields
}

func (*Optional) Exec

func (this *Optional) Exec(transInfo *TransInfo) error

func (*Optional) Rollback

func (this *Optional) Rollback(transInfo *TransInfo)

type Procedure

type Procedure struct {
	Fragments []Fragment
}

func (*Procedure) Exec

func (this *Procedure) Exec(transInfo *TransInfo) error

func (*Procedure) Rollback

func (this *Procedure) Rollback(transInfo *TransInfo)

type Repeat

type Repeat struct {
	MaxTimes int
	TimeLen  time.Duration //ms
	Fragment Fragment
}

func (*Repeat) Exec

func (this *Repeat) Exec(transInfo *TransInfo) error

func (*Repeat) Rollback

func (this *Repeat) Rollback(transInfo *TransInfo)

type Retry

type Retry struct {
	MaxTimes int
	TimeLen  time.Duration //ms
	Fragment Fragment
	Errs     []error
}

func (*Retry) Exec

func (this *Retry) Exec(transInfo *TransInfo) error

func (*Retry) Rollback

func (this *Retry) Rollback(transInfo *TransInfo)

type Specification

type Specification interface {
	Ok(transInfo *TransInfo) bool
}

type Succ

type Succ struct {
}

func (*Succ) Exec

func (this *Succ) Exec(transInfo *TransInfo) error

func (*Succ) Rollback

func (this *Succ) Rollback(transInfo *TransInfo)

type TransInfo

type TransInfo struct {
	//framework paras
	Times   int
	LoopIdx int
	EventId string
	Ch      chan struct{}

	//user info
	AppInfo interface{}
}

type Transaction

type Transaction struct {
	Fragments []Fragment
}

func (*Transaction) HandleEvent

func (this *Transaction) HandleEvent(EventId string, transInfo *TransInfo) error

func (*Transaction) Start

func (this *Transaction) Start(transInfo *TransInfo) error

type Wait

type Wait struct {
	EventId  string
	Timeout  time.Duration //ms
	Fragment Fragment
}

func (*Wait) Exec

func (this *Wait) Exec(transInfo *TransInfo) error

func (*Wait) Rollback

func (this *Wait) Rollback(transInfo *TransInfo)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL