-
Notifications
You must be signed in to change notification settings - Fork 9
/
optimistic_save_test.go
89 lines (78 loc) · 2.39 KB
/
optimistic_save_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package rangedbserver_test
import (
"context"
"encoding/json"
"fmt"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
"github.com/inklabs/rangedb/pkg/clock/provider/sequentialclock"
"github.com/inklabs/rangedb/pkg/grpc/rangedbpb"
"github.com/inklabs/rangedb/pkg/grpc/rangedbserver"
"github.com/inklabs/rangedb/pkg/jsontools"
"github.com/inklabs/rangedb/provider/inmemorystore"
"github.com/inklabs/rangedb/rangedbtest"
)
func ExampleRangeDBServer_OptimisticSave() {
// Given
inMemoryStore := inmemorystore.New(
inmemorystore.WithClock(sequentialclock.New()),
)
rangedbtest.BindEvents(inMemoryStore)
// Setup gRPC server
bufListener := bufconn.Listen(7)
server := grpc.NewServer()
defer server.Stop()
rangeDBServer, err := rangedbserver.New(rangedbserver.WithStore(inMemoryStore))
PrintError(err)
defer rangeDBServer.Stop()
rangedbpb.RegisterRangeDBServer(server, rangeDBServer)
go func() {
PrintError(server.Serve(bufListener))
}()
// Setup gRPC connection
dialer := grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return bufListener.Dial()
})
connCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := grpc.DialContext(connCtx, "bufnet", dialer, grpc.WithInsecure(), grpc.WithBlock())
PrintError(err)
defer Close(conn)
// Setup gRPC client
rangeDBClient := rangedbpb.NewRangeDBClient(conn)
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
request := &rangedbpb.OptimisticSaveRequest{
ExpectedStreamSequenceNumber: 0,
StreamName: "thing-141b39d2b9854f8093ef79dc47dae6af",
Events: []*rangedbpb.Event{
{
AggregateType: "thing",
AggregateID: "141b39d2b9854f8093ef79dc47dae6af",
EventType: "ThingWasDone",
Data: `{"id":"141b39d2b9854f8093ef79dc47dae6af","number":100}`,
Metadata: "",
},
{
AggregateType: "thing",
AggregateID: "141b39d2b9854f8093ef79dc47dae6af",
EventType: "ThingWasDone",
Data: `{"id":"141b39d2b9854f8093ef79dc47dae6af","number":200}`,
Metadata: "",
},
},
}
// When
response, err := rangeDBClient.OptimisticSave(ctx, request)
PrintError(err)
body, err := json.Marshal(response)
PrintError(err)
fmt.Println(jsontools.PrettyJSON(body))
// Output:
// {
// "EventsSaved": 2,
// "LastStreamSequenceNumber": 2
// }
}