-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
64 lines (49 loc) · 1.6 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/google/uuid"
"github.com/modernice/goes/examples/todo"
"github.com/modernice/goes/examples/todo/cmd"
)
func main() {
var setup cmd.Setup
ctx, cancel := setup.Context()
defer cancel()
ebus, _, ereg, disconnect := setup.Events(ctx, "client")
defer disconnect()
cbus, _ := setup.Commands(ereg, ebus)
// Wait a bit to ensure that the todo server is running before dispatching commands.
<-time.After(3 * time.Second)
// Create a new todo list and add some tasks.
listID := uuid.New()
for i := 0; i < 10; i++ {
sleepRandom()
cmd := todo.AddTask(listID, fmt.Sprintf("Task %d", i+1))
if err := cbus.Dispatch(ctx, cmd.Any()); err != nil {
log.Panicf("Failed to dispatch command: %v [cmd=%v, task=%q]", err, cmd.Name(), cmd.Payload())
}
}
// Then remove every second task.
for i := 0; i < 10; i += 2 {
sleepRandom()
cmd := todo.RemoveTask(listID, fmt.Sprintf("Task %d", i+1))
if err := cbus.Dispatch(ctx, cmd.Any()); err != nil {
log.Panicf("Failed to dispatch command: %v [cmd=%v, task=%q]", err, cmd.Name(), cmd.Payload())
}
}
// Remaining tasks: Task 2, Task 4, Task 6, Task 8, Task 10
// Then mark "Task 6" and "Task 10" as done.
sleepRandom()
cmd := todo.DoneTasks(listID, "Task 6", "Task 10")
if err := cbus.Dispatch(ctx, cmd.Any()); err != nil {
log.Panicf("Failed to dispatch command: %v [cmd=%v, tasks=%v]", err, cmd.Name(), cmd.Payload())
}
}
func sleepRandom() {
dur := time.Duration(rand.Intn(1000)) * time.Millisecond
log.Printf("Waiting %s before dispatching next command ...", dur)
time.Sleep(dur)
}