-
Notifications
You must be signed in to change notification settings - Fork 9
/
save_events_with_optimistic_concurrency_test.go
71 lines (61 loc) · 1.54 KB
/
save_events_with_optimistic_concurrency_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
package rangedbapi_test
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/inklabs/rangedb/pkg/clock/provider/sequentialclock"
"github.com/inklabs/rangedb/pkg/jsontools"
"github.com/inklabs/rangedb/pkg/rangedbapi"
"github.com/inklabs/rangedb/provider/inmemorystore"
)
func Example_optimisticSaveEvents() {
// Given
inMemoryStore := inmemorystore.New(
inmemorystore.WithClock(sequentialclock.New()),
)
api, err := rangedbapi.New(rangedbapi.WithStore(inMemoryStore))
PrintError(err)
server := httptest.NewServer(api)
defer server.Close()
serverURL, err := url.Parse(server.URL)
PrintError(err)
serverURL.Path = "/save-events/thing-141b39d2b9854f8093ef79dc47dae6af"
const requestBody = `[
{
"eventType": "ThingWasDone",
"data":{
"id": "141b39d2b9854f8093ef79dc47dae6af",
"number": 100
},
"metadata":null
},
{
"eventType": "ThingWasDone",
"data":{
"id": "141b39d2b9854f8093ef79dc47dae6af",
"number": 200
},
"metadata":null
}
]`
request, err := http.NewRequest(http.MethodPost, serverURL.String(), strings.NewReader(requestBody))
PrintError(err)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("ExpectedStreamSequenceNumber", "0")
client := http.DefaultClient
// When
response, err := client.Do(request)
PrintError(err)
defer Close(response.Body)
body, err := ioutil.ReadAll(response.Body)
PrintError(err)
fmt.Println(jsontools.PrettyJSON(body))
// Output:
// {
// "status": "OK",
// "streamSequenceNumber": 2
// }
}