chore: t.Parallel() + move poll test to own file
This commit is contained in:
parent
231423cd0b
commit
0bdd3dfef8
5 changed files with 77 additions and 48 deletions
|
@ -1,16 +1,12 @@
|
|||
package k2v_test
|
||||
|
||||
import (
|
||||
k2v "code.notaphish.fyi/milas/garage-k2v-go"
|
||||
"context"
|
||||
"github.com/stretchr/testify/require"
|
||||
"math/rand/v2"
|
||||
"net/http/httptrace"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
k2v "code.notaphish.fyi/milas/garage-k2v-go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
type fixture struct {
|
||||
|
@ -22,11 +18,6 @@ type fixture struct {
|
|||
|
||||
func newFixture(t testing.TB) (*fixture, context.Context) {
|
||||
t.Helper()
|
||||
|
||||
t.Cleanup(func() {
|
||||
goleak.VerifyNone(t)
|
||||
})
|
||||
|
||||
ctx := testContext(t)
|
||||
|
||||
cli := k2v.NewClient(k2v.EndpointFromEnv(), k2v.KeyFromEnv())
|
||||
|
@ -53,18 +44,21 @@ func randomKey() string {
|
|||
}
|
||||
|
||||
func TestClient_InsertItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, ctx := newFixture(t)
|
||||
err := f.cli.InsertItem(ctx, f.bucket, randomKey(), randomKey(), "", []byte("hello"))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestClient_ReadItemNotExist(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
sk := randomKey()
|
||||
|
||||
t.Run("Single", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
item, ct, err := f.cli.ReadItemSingle(ctx, f.bucket, pk, sk)
|
||||
require.ErrorIs(t, err, k2v.NoSuchItemErr)
|
||||
require.Nil(t, item)
|
||||
|
@ -72,6 +66,7 @@ func TestClient_ReadItemNotExist(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("Multi", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
items, ct, err := f.cli.ReadItemMulti(ctx, f.bucket, pk, sk)
|
||||
require.ErrorIs(t, err, k2v.NoSuchItemErr)
|
||||
require.Empty(t, items)
|
||||
|
@ -80,6 +75,7 @@ func TestClient_ReadItemNotExist(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestClient_ReadItemTombstone(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
|
@ -110,6 +106,7 @@ func TestClient_ReadItemTombstone(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestClient_ReadItemSingleRevision(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
|
@ -119,6 +116,7 @@ func TestClient_ReadItemSingleRevision(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
t.Run("Single", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
item, ct, err := f.cli.ReadItemSingle(ctx, f.bucket, pk, sk)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello", string(item))
|
||||
|
@ -126,6 +124,7 @@ func TestClient_ReadItemSingleRevision(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("Multi", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
items, ct, err := f.cli.ReadItemMulti(ctx, f.bucket, pk, sk)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, items, 1)
|
||||
|
@ -135,6 +134,7 @@ func TestClient_ReadItemSingleRevision(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestClient_ReadItemMultipleRevisions(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
|
@ -148,6 +148,7 @@ func TestClient_ReadItemMultipleRevisions(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
t.Run("Single", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
item, ct, err := f.cli.ReadItemSingle(ctx, f.bucket, pk, sk)
|
||||
require.ErrorIs(t, err, k2v.ConcurrentItemsErr)
|
||||
require.Nil(t, item)
|
||||
|
@ -155,6 +156,7 @@ func TestClient_ReadItemMultipleRevisions(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("Multi", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
items, ct, err := f.cli.ReadItemMulti(ctx, f.bucket, pk, sk)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, items, 2)
|
||||
|
@ -163,38 +165,3 @@ func TestClient_ReadItemMultipleRevisions(t *testing.T) {
|
|||
require.NotEmpty(t, ct)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_PollItem(t *testing.T) {
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
sk := randomKey()
|
||||
|
||||
err := f.cli.InsertItem(ctx, f.bucket, pk, sk, "", []byte("hello1"))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, ct, err := f.cli.ReadItemSingle(ctx, f.bucket, pk, sk)
|
||||
|
||||
pollReadyCh := make(chan struct{})
|
||||
go func(ct k2v.CausalityToken) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Errorf("Context canceled: %v", ctx.Err())
|
||||
return
|
||||
case <-pollReadyCh:
|
||||
t.Logf("PollItem connected")
|
||||
}
|
||||
err = f.cli.InsertItem(ctx, f.bucket, pk, sk, ct, []byte("hello2"))
|
||||
require.NoError(t, err)
|
||||
}(ct)
|
||||
|
||||
trace := &httptrace.ClientTrace{
|
||||
WroteRequest: func(_ httptrace.WroteRequestInfo) {
|
||||
pollReadyCh <- struct{}{}
|
||||
},
|
||||
}
|
||||
item, ct, err := f.cli.PollItem(httptrace.WithClientTrace(ctx, trace), f.bucket, pk, sk, ct, 5*time.Second)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello2", string(item))
|
||||
require.NotEmpty(t, ct)
|
||||
}
|
||||
|
|
4
go.mod
4
go.mod
|
@ -3,14 +3,14 @@ module code.notaphish.fyi/milas/garage-k2v-go
|
|||
go 1.23.1
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.32.2
|
||||
github.com/aws/aws-sdk-go-v2 v1.36.3
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/stretchr/testify v1.8.0
|
||||
go.uber.org/goleak v1.3.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/aws/smithy-go v1.22.0 // indirect
|
||||
github.com/aws/smithy-go v1.22.3 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,7 +1,11 @@
|
|||
github.com/aws/aws-sdk-go-v2 v1.32.2 h1:AkNLZEyYMLnx/Q/mSKkcMqwNFXMAvFto9bNsHqcTduI=
|
||||
github.com/aws/aws-sdk-go-v2 v1.32.2/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo=
|
||||
github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM=
|
||||
github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg=
|
||||
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
|
||||
github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
|
||||
github.com/aws/smithy-go v1.22.3 h1:Z//5NuZCSW6R4PhQ93hShNbyBbn8BWCmCVCt+Q8Io5k=
|
||||
github.com/aws/smithy-go v1.22.3/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
|
10
main_test.go
Normal file
10
main_test.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package k2v
|
||||
|
||||
import (
|
||||
"go.uber.org/goleak"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
goleak.VerifyTestMain(m)
|
||||
}
|
48
poll_single_test.go
Normal file
48
poll_single_test.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package k2v_test
|
||||
|
||||
import (
|
||||
k2v "code.notaphish.fyi/milas/garage-k2v-go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/http/httptrace"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestClient_PollItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
f, ctx := newFixture(t)
|
||||
|
||||
pk := randomKey()
|
||||
sk := randomKey()
|
||||
|
||||
err := f.cli.InsertItem(ctx, f.bucket, pk, sk, "", []byte("hello1"))
|
||||
require.NoError(t, err)
|
||||
|
||||
_, ct, err := f.cli.ReadItemSingle(ctx, f.bucket, pk, sk)
|
||||
|
||||
updateErrCh := make(chan error, 1)
|
||||
pollReadyCh := make(chan struct{})
|
||||
go func(ct k2v.CausalityToken) {
|
||||
defer close(updateErrCh)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Errorf("Context canceled: %v", ctx.Err())
|
||||
return
|
||||
case <-pollReadyCh:
|
||||
t.Logf("PollItem connected")
|
||||
}
|
||||
updateErrCh <- f.cli.InsertItem(ctx, f.bucket, pk, sk, ct, []byte("hello2"))
|
||||
}(ct)
|
||||
|
||||
trace := &httptrace.ClientTrace{
|
||||
WroteRequest: func(_ httptrace.WroteRequestInfo) {
|
||||
pollReadyCh <- struct{}{}
|
||||
},
|
||||
}
|
||||
item, ct, err := f.cli.PollItem(httptrace.WithClientTrace(ctx, trace), f.bucket, pk, sk, ct, 5*time.Second)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello2", string(item))
|
||||
require.NotEmpty(t, ct)
|
||||
require.NoError(t, <-updateErrCh)
|
||||
}
|
Loading…
Add table
Reference in a new issue