Skip to content

Commit c4be366

Browse files
authored
[Fix] Cleanup collections with defer (#262)
1 parent f3a9751 commit c4be366

File tree

10 files changed

+73
-19
lines changed

10 files changed

+73
-19
lines changed

test/agency_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ import (
2626
"context"
2727
"crypto/tls"
2828
"fmt"
29+
"net/http"
30+
"os"
31+
"reflect"
32+
"strings"
33+
"testing"
34+
"time"
35+
2936
driver "github.com/arangodb/go-driver"
3037
"github.com/arangodb/go-driver/agency"
3138
httpdriver "github.com/arangodb/go-driver/http"
3239
"github.com/arangodb/go-driver/jwt"
3340
"github.com/arangodb/go-driver/util"
3441
"github.com/stretchr/testify/assert"
3542
"github.com/stretchr/testify/require"
36-
"net/http"
37-
"os"
38-
"reflect"
39-
"strings"
40-
"testing"
41-
"time"
4243
)
4344

4445
// getAgencyEndpoints queries the cluster to get all agency endpoints.

test/clean.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package test
24+
25+
import (
26+
"context"
27+
"testing"
28+
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
type remove interface {
33+
Remove(ctx context.Context) error
34+
}
35+
36+
func clean(t *testing.T, ctx context.Context, col remove) {
37+
require.NoError(t, col.Remove(ctx))
38+
}

test/client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ func TestResponseHeader(t *testing.T) {
397397
var resp driver.Response
398398
db := ensureDatabase(ctx, c, "_system", nil, t)
399399
col := ensureCollection(ctx, db, "response_header_test", nil, t)
400+
defer clean(t, ctx, col)
400401

401402
// `ETag` header must contain the `_rev` of the new document in quotes.
402403
doc := map[string]string{

test/cluster_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ func TestClusterDatabaseInventorySatellite(t *testing.T) {
133133
if err != nil {
134134
t.Fatalf("Failed to open _system database: %s", describe(err))
135135
}
136-
ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
136+
col := ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
137137
ReplicationFactor: driver.ReplicationFactorSatellite,
138138
}, t)
139+
defer clean(t, ctx, col)
139140
h, err := cl.Health(ctx)
140141
if err != nil {
141142
t.Fatalf("Health failed: %s", describe(err))
@@ -185,11 +186,12 @@ func TestClusterDatabaseInventorySmartJoin(t *testing.T) {
185186
if err != nil {
186187
t.Fatalf("Failed to open _system database: %s", describe(err))
187188
}
188-
ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
189+
col := ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
189190
ShardKeys: []string{"_key:"},
190191
SmartJoinAttribute: "smart",
191192
NumberOfShards: 2,
192193
}, t)
194+
defer clean(t, ctx, col)
193195
inv, err := cl.DatabaseInventory(ctx, db)
194196
if err != nil {
195197
t.Fatalf("DatabaseInventory failed: %s", describe(err))
@@ -225,9 +227,10 @@ func TestClusterDatabaseInventoryShardingStrategy(t *testing.T) {
225227
if err != nil {
226228
t.Fatalf("Failed to open _system database: %s", describe(err))
227229
}
228-
ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
230+
col := ensureCollection(ctx, db, name, &driver.CreateCollectionOptions{
229231
ShardingStrategy: driver.ShardingStrategyCommunityCompat,
230232
}, t)
233+
defer clean(t, ctx, col)
231234
inv, err := cl.DatabaseInventory(ctx, db)
232235
if err != nil {
233236
t.Fatalf("DatabaseInventory failed: %s", describe(err))
@@ -261,6 +264,7 @@ func TestClusterMoveShard(t *testing.T) {
261264
col, err := db.CreateCollection(ctx, "test_move_shard", &driver.CreateCollectionOptions{
262265
NumberOfShards: 12,
263266
})
267+
defer clean(t, ctx, col)
264268
if err != nil {
265269
t.Fatalf("CreateCollection failed: %s", describe(err))
266270
}
@@ -361,6 +365,7 @@ func TestClusterResignLeadership(t *testing.T) {
361365
NumberOfShards: 12,
362366
ReplicationFactor: 2,
363367
})
368+
defer clean(t, ctx, col)
364369
if err != nil {
365370
t.Fatalf("CreateCollection failed: %s", describe(err))
366371
}
@@ -448,6 +453,7 @@ func TestClusterMoveShardWithViews(t *testing.T) {
448453
col, err := db.CreateCollection(ctx, "test_move_shard_with_view", &driver.CreateCollectionOptions{
449454
NumberOfShards: 12,
450455
})
456+
clean(t, ctx, col)
451457
if err != nil {
452458
t.Fatalf("CreateCollection failed: %s", describe(err))
453459
}
@@ -457,7 +463,9 @@ func TestClusterMoveShardWithViews(t *testing.T) {
457463
},
458464
}
459465
viewName := "test_move_shard_view"
460-
if _, err := db.CreateArangoSearchView(ctx, viewName, opts); err != nil {
466+
view, err := db.CreateArangoSearchView(ctx, viewName, opts)
467+
clean(t, ctx, view)
468+
if err != nil {
461469
t.Fatalf("Failed to create view '%s': %s", viewName, describe(err))
462470
}
463471
h, err := cl.Health(ctx)

test/database_collection_defaults_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
package test
2424

2525
import (
26-
"github.com/dchest/uniuri"
2726
"strings"
2827
"testing"
2928

29+
"github.com/dchest/uniuri"
30+
3031
"github.com/arangodb/go-driver"
3132
"github.com/stretchr/testify/require"
3233
)

test/database_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ package test
2525
import (
2626
"context"
2727
"fmt"
28-
"github.com/dchest/uniuri"
29-
"github.com/stretchr/testify/require"
3028
"strings"
3129
"testing"
3230

31+
"github.com/dchest/uniuri"
32+
"github.com/stretchr/testify/require"
33+
3334
"github.com/arangodb/go-driver"
3435
)
3536

test/foxx_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ package test
2323

2424
import (
2525
"context"
26-
"github.com/arangodb/go-driver"
27-
"github.com/stretchr/testify/require"
2826
"os"
2927
"testing"
3028
"time"
29+
30+
"github.com/arangodb/go-driver"
31+
"github.com/stretchr/testify/require"
3132
)
3233

3334
func TestFoxxItzpapalotlService(t *testing.T) {

test/graph_creation_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ package test
2424

2525
import (
2626
"context"
27-
"github.com/arangodb/go-driver"
28-
"github.com/stretchr/testify/require"
2927
"testing"
3028
"time"
29+
30+
"github.com/arangodb/go-driver"
31+
"github.com/stretchr/testify/require"
3132
)
3233

3334
func newGraphOpts(db driver.Database) (driver.CreateGraphOptions, []string) {

test/revisions_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"github.com/arangodb/go-driver"
8-
"github.com/stretchr/testify/require"
97
"net/http"
108
"testing"
119
"time"
10+
11+
"github.com/arangodb/go-driver"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestRevisionTree(t *testing.T) {

test/server_mode_auth_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func TestServerModeAndGrants(t *testing.T) {
6262
db := ensureDatabase(ctx, c, "_system", nil, t)
6363
colName := "server_mode_and_grants_test1"
6464
col := ensureCollection(ctx, db, colName, nil, t)
65+
clean(t, ctx, col)
6566

6667
// Get database & collection access
6768
defaultDBAccess, err := u.GetDatabaseAccess(ctx, db)

0 commit comments

Comments
 (0)