-
Notifications
You must be signed in to change notification settings - Fork 9
/
delete_stream_with_optimistic_concurrency_failure_test.go
64 lines (53 loc) · 1.64 KB
/
delete_stream_with_optimistic_concurrency_failure_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
package rangedbapi_test
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"time"
"github.com/inklabs/rangedb"
"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"
"github.com/inklabs/rangedb/rangedbtest"
)
func Example_optimisticDeleteStream_failure() {
// Given
inMemoryStore := inmemorystore.New(
inmemorystore.WithClock(sequentialclock.New()),
)
api, err := rangedbapi.New(rangedbapi.WithStore(inMemoryStore))
PrintError(err)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
streamName := "thing-4b9a415c53734b69ac459a7e53eb4c1b"
PrintError(IgnoreFirstNumber(inMemoryStore.Save(ctx, streamName,
&rangedb.EventRecord{Event: rangedbtest.ThingWasDone{ID: "4b9a415c53734b69ac459a7e53eb4c1b", Number: 100}},
)))
server := httptest.NewServer(api)
defer server.Close()
serverURL, err := url.Parse(server.URL)
PrintError(err)
serverURL.Path = "/delete-stream/thing-4b9a415c53734b69ac459a7e53eb4c1b"
request, err := http.NewRequest(http.MethodPost, serverURL.String(), nil)
PrintError(err)
request.Header.Set("ExpectedStreamSequenceNumber", "2")
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(response.Status)
fmt.Println(jsontools.PrettyJSON(body))
// Output:
// 409 Conflict
// {
// "status": "Failed",
// "message": "unexpected sequence number: 2, actual: 1"
// }
}