-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
50 lines (43 loc) · 1.04 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package pubsub_test
import (
"context"
"fmt"
"time"
"github.com/DoNewsCode/core"
pubsub "github.com/DoNewsCode/core-pubsub"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
)
type MyModule struct {
goch *gochannel.GoChannel
cancel func()
}
func (m *MyModule) ProvidePubSub(router *message.Router) {
router.AddNoPublisherHandler(
"example",
"example-in",
m.goch,
func(msg *message.Message) error {
fmt.Println(string(msg.Payload))
m.cancel()
return nil
},
)
}
func Example() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
goch := gochannel.NewGoChannel(gochannel.Config{}, watermill.NopLogger{})
module := MyModule{goch, cancel}
c := core.Default(core.WithInline("log.level", "none"))
c.AddModuleFunc(pubsub.New)
c.AddModule(&module)
go func() {
time.Sleep(time.Second)
goch.Publish("example-in", message.NewMessage(watermill.NewUUID(), message.Payload("foo")))
}()
c.Serve(ctx)
// Output:
// foo
}