Skip to content

Commit 308a653

Browse files
committed
Create agency subkeys
1 parent b6946b6 commit 308a653

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ services:
2828
language: go
2929

3030
env:
31-
- TEST_SUITE=run-tests-http
31+
- TEST_SUITE=run-unit-tests
3232
- TEST_SUITE=run-tests-single ARANGODB=arangodb:3.5
3333
- TEST_SUITE=run-tests-single ARANGODB=arangodb/arangodb:latest
3434
- TEST_SUITE=run-tests-single ARANGODB=arangodb/arangodb-preview:latest

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,19 @@ changelog:
128128
--no-author \
129129
--unreleased-label "Master"
130130

131-
run-tests: run-tests-http run-tests-single run-tests-resilientsingle run-tests-cluster
131+
run-tests: run-unit-tests run-tests-single run-tests-resilientsingle run-tests-cluster
132132

133-
# Tests of HTTP package
134-
run-tests-http:
133+
# The below rule exists only for backward compatibility.
134+
run-tests-http: run-unit-tests
135+
136+
run-unit-tests:
135137
@docker run \
136138
--rm \
137139
-v "${ROOTDIR}":/usr/code \
138140
-e CGO_ENABLED=0 \
139141
-w /usr/code/ \
140142
golang:$(GOVERSION) \
141-
go test $(TESTOPTIONS) $(REPOPATH)/http
143+
go test $(TESTOPTIONS) $(REPOPATH)/http $(REPOPATH)/agency
142144

143145
# Single server tests
144146
run-tests-single: run-tests-single-json run-tests-single-vpack run-tests-single-vst-1.0 $(VST11_SINGLE_TESTS)

agency/operation.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ package agency
2424

2525
import "time"
2626

27+
type Key []string
28+
2729
// KeyChanger describes how operation should be performed on a key in the agency
2830
type KeyChanger interface {
2931
// GetKey returns which key must be changed
@@ -41,7 +43,18 @@ type KeyChanger interface {
4143
}
4244

4345
type keyCommon struct {
44-
key []string
46+
key Key
47+
}
48+
49+
// CreateSubKey creates new key based on receiver key.
50+
// Returns new key with new allocated memory.
51+
func (k Key) CreateSubKey(elements ...string) Key {
52+
NewKey := make([]string, 0, len(k)+len(elements))
53+
54+
NewKey = append(NewKey, k...)
55+
NewKey = append(NewKey, elements...)
56+
57+
return NewKey
4558
}
4659

4760
func (k *keyCommon) GetKey() string {

agency/operation_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package agency_test
2+
3+
import (
4+
"github.com/arangodb/go-driver/agency"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
)
8+
9+
func TestCreateSubKey(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
elements []string
13+
key agency.Key
14+
}{
15+
{
16+
name: "Create a new key based on not empty key with not empty elements",
17+
key: agency.Key{"level1", "level2"},
18+
elements: []string{"level3"},
19+
},
20+
{
21+
name: "Create a new key based on not empty key with empty elements",
22+
key: agency.Key{"level1", "level2"},
23+
},
24+
{
25+
name: "Create a new key based on empty key",
26+
elements: []string{"level3"},
27+
},
28+
{
29+
name: "Create a new key based on empty key with empty elements",
30+
},
31+
}
32+
33+
for _, testCase := range testCases {
34+
t.Run(testCase.name, func(t *testing.T) {
35+
newKey := testCase.key.CreateSubKey(testCase.elements...)
36+
37+
require.Len(t, newKey, len(testCase.key)+len(testCase.elements))
38+
if len(testCase.key) > 0 && &testCase.key[0] == &newKey[0] {
39+
require.Fail(t, "New key should have always different address")
40+
}
41+
42+
for i, s := range testCase.key {
43+
require.Equal(t, s, newKey[i])
44+
}
45+
for i, s := range testCase.elements {
46+
require.Equal(t, s, newKey[i+len(testCase.key)])
47+
}
48+
})
49+
}
50+
51+
}

0 commit comments

Comments
 (0)