doc(event): update event doc

This commit is contained in:
bandl 2021-09-28 20:59:54 +08:00
parent 0b8e2a104f
commit f53eea9c22
1 changed files with 44 additions and 48 deletions

View File

@ -90,63 +90,59 @@ func TestNewConsumer(t *testing.T) {
```go
func TestNewConsumer(t *testing.T) {
// 定义一个图书馆
type Library struct {
driver DriverInterface
}
library := &Library{
driver: NewDriver(100),
}
ctx := context.Background()
// 定义一个图书馆
type Library struct {
driver DriverInterface
}
library := &Library{
driver: NewDriver(100),
}
ctx := context.Background()
// 定义 A
type A struct {
produce ProduceInterface
}
// 定义 A
type A struct {
produce ProduceInterface
}
a := &A{
produce: NewProduce(library.driver),
}
a := &A{
produce: NewProduce(library.driver),
}
// 定义 B
type B struct {
consumer ConsumerInterface
}
// 定义 B
type B struct {
consumer ConsumerInterface
}
b := &B{
consumer: NewConsumer(library.driver),
}
b := &B{
consumer: NewConsumer(library.driver),
}
// 定义书 S 并且添加一些描述
book := NewEvent("S")
book.SetMsg("title", "hello world")
book.SetCtxValue("pages", 120)
// 定义书 S 并且添加一些描述
book := NewEvent("S")
book.SetMsg("title", "hello world")
book.SetValue("pages", 120)
// A 把书 S 放到图书馆
go func() {
waitMsg, err := book.UpdateWaitEvent(2 * time.Hour)
require.NoError(t, err)
a.produce.Call(ctx, book)
// A 把书 S 放到图书馆
go func() {
book.InitWaitEvent()
a.produce.Call(ctx, book)
// A 等待 B 的回复, 但是他最多只会等待 B 2个小时
ttlTime, err := book.GetTtlTimer()
require.NoError(t, err)
select {
case msg := <-waitMsg:
fmt.Println(msg)
case <-ttlTime.C:
fmt.Println("过期不等了")
book.Close()
}
}()
// A 等待 B 的回复, 但是他最多只会等待 B 2个小时
res, err := book.StartWaitEvent(2 * time.Hour)
require.NoError(t, err)
fmt.Println(res)
}()
// 模拟 B 去图书馆拿书
book = b.consumer.Receive(ctx)
fmt.Println(book.GetMsg("title"))
// 模拟 B 去图书馆拿书
book = b.consumer.Receive(ctx)
fmt.Println(book.GetMsg("title"))
// 书完好
book.SetWaitResult("书完好")
time.Sleep(50 * time.Millisecond)
// 书完好
book.ExecWorkAndSendResult(func() (interface{}, error) {
// b 检查书
return "OK", nil
})
time.Sleep(50 * time.Millisecond)
}
```