Skip to content

Commit

Permalink
refactor: refactored instagocb unit test to fix random failures (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
Angith authored Sep 19, 2024
1 parent 4cecdbd commit d62c434
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 91 deletions.
95 changes: 39 additions & 56 deletions instrumentation/instagocb/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,32 @@ func TestCollection_CRUD(t *testing.T) {
testDocumentValue := getTestDocumentValue()
defer instana.ShutdownSensor()

recorder, ctx, cluster, a := prepareWithCollection(t, testBucketName, testScope, testCollection)
recorder, ctx, cluster, a := prepare(t)
defer cluster.Close(&gocb.ClusterCloseOptions{})

collection := cluster.Bucket(testBucketName).Scope(testScope).Collection(testCollection)

// Insert
_, err := collection.Insert(testDocumentID, testDocumentValue, &gocb.InsertOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span := getLatestSpan(recorder)
a.Equal(0, span.Ec)
a.EqualValues(instana.ExitSpanKind, span.Kind)
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data := span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "INSERT",
Error: "",
}, data.Tags)
collection := cluster.Bucket(cbTestBucket).Scope(cbTestScope).Collection(cbTestCollection)

// Get
var result myDoc
res, err := collection.Get(testDocumentID, &gocb.GetOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
res, err := collection.Get(crudTestDocumentID, &gocb.GetOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)
res.Content(&result)
a.Equal(testDocumentValue, result)
span = getLatestSpan(recorder)
span := getLatestSpan(recorder)
a.Equal(0, span.Ec)
a.EqualValues(instana.ExitSpanKind, span.Kind)
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
data := span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "GET",
Error: "",
}, data.Tags)

// Upsert
_, err = collection.Upsert(testDocumentID, &myDoc{}, &gocb.UpsertOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.Upsert(crudTestDocumentID, &myDoc{}, &gocb.UpsertOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -78,15 +61,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "UPSERT",
Error: "",
}, data.Tags)

// Replace
_, err = collection.Replace(testDocumentID, "newValue2", &gocb.ReplaceOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.Replace(crudTestDocumentID, "newValue2", &gocb.ReplaceOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -95,15 +78,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "REPLACE",
Error: "",
}, data.Tags)

// Exists
res1, err := collection.Exists(testDocumentID, &gocb.ExistsOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
res1, err := collection.Exists(crudTestDocumentID, &gocb.ExistsOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)
a.True(res1.Exists())

Expand All @@ -113,15 +96,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "EXISTS",
Error: "",
}, data.Tags)

// GetAllReplicas
_, err = collection.GetAllReplicas(testDocumentID, &gocb.GetAllReplicaOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.GetAllReplicas(crudTestDocumentID, &gocb.GetAllReplicaOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -130,15 +113,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "GET_ALL_REPLICAS",
Error: "",
}, data.Tags)

// GetAnyReplica
_, err = collection.GetAnyReplica(testDocumentID, &gocb.GetAnyReplicaOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.GetAnyReplica(crudTestDocumentID, &gocb.GetAnyReplicaOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -147,15 +130,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "GET_ANY_REPLICA",
Error: "",
}, data.Tags)

// GetAndTouch
_, err = collection.GetAndTouch(testDocumentID, time.Minute*20, &gocb.GetAndTouchOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.GetAndTouch(crudTestDocumentID, time.Minute*20, &gocb.GetAndTouchOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -164,15 +147,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "GET_AND_TOUCH",
Error: "",
}, data.Tags)

// GetAndLock
ress, err := collection.GetAndLock(testDocumentID, time.Minute*20, &gocb.GetAndLockOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
ress, err := collection.GetAndLock(crudTestDocumentID, time.Minute*20, &gocb.GetAndLockOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -181,15 +164,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "GET_AND_LOCK",
Error: "",
}, data.Tags)

// Unlock
err = collection.Unlock(testDocumentID, ress.Cas(), &gocb.UnlockOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
err = collection.Unlock(crudTestDocumentID, ress.Cas(), &gocb.UnlockOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -198,15 +181,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "UNLOCK",
Error: "",
}, data.Tags)

// Touch
_, err = collection.Touch(testDocumentID, time.Minute*20, &gocb.TouchOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.Touch(crudTestDocumentID, time.Minute*20, &gocb.TouchOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -215,15 +198,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "TOUCH",
Error: "",
}, data.Tags)

// LookupIn
_, err = collection.LookupIn(testDocumentID, []gocb.LookupInSpec{
_, err = collection.LookupIn(crudTestDocumentID, []gocb.LookupInSpec{
gocb.GetSpec("test", &gocb.GetSpecOptions{}),
}, &gocb.LookupInOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)
Expand All @@ -234,17 +217,17 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "LOOKUP_IN",
Error: "",
}, data.Tags)

// MutateIn
_, err = collection.Upsert(testDocumentID, testDocumentValue, &gocb.UpsertOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.Upsert(crudTestDocumentID, testDocumentValue, &gocb.UpsertOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)
_, err = collection.MutateIn(testDocumentID, []gocb.MutateInSpec{
_, err = collection.MutateIn(crudTestDocumentID, []gocb.MutateInSpec{
gocb.UpsertSpec("foo", "311-555-0151", &gocb.UpsertSpecOptions{}),
}, &gocb.MutateInOptions{
ParentSpan: instagocb.GetParentSpanFromContext(ctx),
Expand All @@ -257,7 +240,7 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "MUTATE_IN",
Expand All @@ -268,7 +251,7 @@ func TestCollection_CRUD(t *testing.T) {
bc := collection.Binary()

// Append
_, err = bc.Append(testDocumentID, []byte{23}, &gocb.AppendOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = bc.Append(crudTestDocumentID, []byte{23}, &gocb.AppendOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -277,15 +260,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "APPEND",
Error: "",
}, data.Tags)

// Prepend
_, err = bc.Prepend(testDocumentID, []byte{23}, &gocb.PrependOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = bc.Prepend(crudTestDocumentID, []byte{23}, &gocb.PrependOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -294,15 +277,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "PREPEND",
Error: "",
}, data.Tags)

// Remove
_, err = collection.Remove(testDocumentID, &gocb.RemoveOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
_, err = collection.Remove(crudTestDocumentID, &gocb.RemoveOptions{ParentSpan: instagocb.GetParentSpanFromContext(ctx)})
a.NoError(err)

span = getLatestSpan(recorder)
Expand All @@ -311,15 +294,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "REMOVE",
Error: "",
}, data.Tags)

// Increment
_, err = bc.Increment(testDocumentID, &gocb.IncrementOptions{
_, err = bc.Increment(crudTestDocumentID, &gocb.IncrementOptions{
Initial: 2,
ParentSpan: instagocb.GetParentSpanFromContext(ctx),
})
Expand All @@ -331,15 +314,15 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "INCREMENT",
Error: "",
}, data.Tags)

// Decrement
_, err = bc.Decrement(testDocumentID, &gocb.DecrementOptions{
_, err = bc.Decrement(crudTestDocumentID, &gocb.DecrementOptions{
Initial: 2,
ParentSpan: instagocb.GetParentSpanFromContext(ctx),
})
Expand All @@ -351,7 +334,7 @@ func TestCollection_CRUD(t *testing.T) {
a.IsType(instana.CouchbaseSpanData{}, span.Data)
data = span.Data.(instana.CouchbaseSpanData)
a.Equal(instana.CouchbaseSpanTags{
Bucket: testBucketName,
Bucket: cbTestBucket,
Host: "localhost",
Type: string(gocb.CouchbaseBucketType),
SQL: "DECREMENT",
Expand Down
22 changes: 13 additions & 9 deletions instrumentation/instagocb/instagocb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ var (

// test document IDs
var (
testDocumentID = "test-doc-id"
clusterTestDocumentID = "cluster-test-doc-id"
dsListTestDocumentID = "ds-list-test-doc-id"
dsMapTestDocumentID = "ds-map-test-doc-id"
dsQueueTestDocumentID = "ds-queue-test-doc-id"
dsSetTestDocumentID = "ds-set-test-doc-id"
scopeTestDocumentID = "scope-test-doc-id"
testDocumentID = "test-doc-id"
clusterTestDocumentID = "cluster-test-doc-id"
dsListTestDocumentID = "ds-list-test-doc-id"
dsMapTestDocumentID = "ds-map-test-doc-id"
dsQueueTestDocumentID = "ds-queue-test-doc-id"
dsSetTestDocumentID = "ds-set-test-doc-id"
scopeTestDocumentID = "scope-test-doc-id"
crudTestDocumentID = "crud-test-doc-id"
transactionTestDocumentID = "transaction-test-doc-id"
)

var rec *instana.Recorder
Expand Down Expand Up @@ -232,8 +234,10 @@ func setup() error {
dsMapTestDocumentID: map[string]interface{}{
"test-key": getTestStringValue(),
},
scopeTestDocumentID: getTestDocumentValue(),
clusterTestDocumentID: getTestDocumentValue(),
scopeTestDocumentID: getTestDocumentValue(),
clusterTestDocumentID: getTestDocumentValue(),
crudTestDocumentID: getTestDocumentValue(),
transactionTestDocumentID: getTestDocumentValue(),
}

// Create an Array of BulkOps for Insert
Expand Down
Loading

0 comments on commit d62c434

Please sign in to comment.