-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_registry_test.go
83 lines (67 loc) · 2.57 KB
/
bucket_registry_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
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReuseInMemoryBucket(t *testing.T) {
ensureNoLeaks(t)
bucketName := strings.ToLower(t.Name())
bucket1, err := OpenBucket(InMemoryURL, bucketName, CreateOrOpen)
require.NoError(t, err)
key := "foo"
body := []byte("bar")
require.NoError(t, bucket1.DefaultDataStore().Set("foo", 0, nil, body))
require.False(t, bucket1.closed)
bucket1.Close(testCtx(t))
defer func() {
assert.NoError(t, bucket1.CloseAndDelete(testCtx(t)))
assert.Len(t, GetBucketNames(), 0)
}()
require.True(t, bucket1.closed)
require.Equal(t, []string{bucketName}, GetBucketNames())
bucket2, err := OpenBucket(InMemoryURL, bucketName, CreateOrOpen)
require.NoError(t, err)
require.False(t, bucket2.closed)
require.Equal(t, []string{bucketName}, GetBucketNames())
var bucket2Body []byte
_, err = bucket2.DefaultDataStore().Get(key, &bucket2Body)
require.NoError(t, err)
require.Equal(t, body, bucket2Body)
}
func TestBucketRegistryRefCountPersistentBucket(t *testing.T) {
ensureNoLeaks(t)
bucketName := strings.ToLower(t.Name())
bucket, err := OpenBucket(uriFromPath(t.TempDir()+"/"+bucketName), bucketName, CreateOrOpen)
require.NoError(t, err)
require.Equal(t, []string{bucketName}, GetBucketNames())
bucket.Close(testCtx(t))
require.Len(t, GetBucketNames(), 0)
}
func TestDuplicateBucketNamesDifferentPath(t *testing.T) {
ensureNoLeaks(t)
bucketName := strings.ToLower(t.Name())
path1 := uriFromPath(t.TempDir() + "/" + bucketName + "1")
path2 := uriFromPath(t.TempDir() + "/" + bucketName + "2")
bucket1, err := OpenBucket(path1, bucketName, CreateOrOpen)
require.NoError(t, err)
require.Equal(t, []string{bucketName}, GetBucketNames())
bucket2, err := OpenBucket(path2, bucketName, CreateOrOpen)
require.ErrorContains(t, err, "already exists")
require.Equal(t, []string{bucketName}, GetBucketNames())
bucket1.Close(testCtx(t))
require.Len(t, GetBucketNames(), 0)
// Close bucket1, should allow bucket2 to open
bucket2, err = OpenBucket(path2, bucketName, CreateOrOpen)
require.NoError(t, err)
defer bucket2.Close(testCtx(t))
require.Equal(t, []string{bucketName}, GetBucketNames())
}