Skip to content

Commit cd71310

Browse files
authored
[Feature] Batch extension should not work with a copy (#313)
1 parent e63cef3 commit cd71310

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

MAINTAINERS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
- `make run-tests-single`
99
- `make run-tests-resilientsingle`
1010
- `make run-tests-cluster`.
11+
- The test can be launched with the flag `RACE=on` which means that test will be performed with the race detector, e.g:
12+
- `RACE=on make run-tests-single`
1113
- Always create changes in a PR

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ ifdef VERBOSE
2424
TESTVERBOSEOPTIONS := -v
2525
endif
2626

27+
CGO_ENABLED=0
28+
ifdef RACE
29+
TESTVERBOSEOPTIONS += -race
30+
CGO_ENABLED=1
31+
endif
32+
33+
2734
ORGPATH := github.com/arangodb
2835
REPONAME := $(PROJECT)
2936
REPODIR := $(ORGDIR)/$(REPONAME)
@@ -142,7 +149,7 @@ run-unit-tests:
142149
@$(DOCKER_CMD) \
143150
--rm \
144151
-v "${ROOTDIR}":/usr/code \
145-
-e CGO_ENABLED=0 \
152+
-e CGO_ENABLED=$(CGO_ENABLED) \
146153
-w /usr/code/ \
147154
$(GOIMAGE) \
148155
go test $(TESTOPTIONS) $(REPOPATH)/http $(REPOPATH)/agency
@@ -352,7 +359,7 @@ __test_go_test:
352359
-e TEST_BACKUP_REMOTE_CONFIG='$(TEST_BACKUP_REMOTE_CONFIG)' \
353360
-e TEST_DEBUG='$(TEST_DEBUG)' \
354361
-e GODEBUG=tls13=1 \
355-
-e CGO_ENABLED=0 \
362+
-e CGO_ENABLED=$(CGO_ENABLED) \
356363
-w /usr/code/ \
357364
$(GOIMAGE) \
358365
go test $(GOBUILDTAGSOPT) $(TESTOPTIONS) $(TESTVERBOSEOPTIONS) $(TESTS)
@@ -373,7 +380,7 @@ __test_v2_go_test:
373380
-e TEST_BACKUP_REMOTE_CONFIG='$(TEST_BACKUP_REMOTE_CONFIG)' \
374381
-e TEST_DEBUG='$(TEST_DEBUG)' \
375382
-e GODEBUG=tls13=1 \
376-
-e CGO_ENABLED=0 \
383+
-e CGO_ENABLED=$(CGO_ENABLED) \
377384
-w /usr/code/v2/ \
378385
$(GOV2IMAGE) \
379386
go test $(GOBUILDTAGSOPT) $(TESTOPTIONS) $(TESTVERBOSEOPTIONS) ./tests

replication_impl.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2018-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Ewout Prangsma
21+
// Author Tomasz Mielech
2122
//
2223

2324
package driver
@@ -110,7 +111,7 @@ func (b batchMetadata) LastTick() Tick {
110111
}
111112

112113
// Extend the lifetime of an existing batch on the server
113-
func (b batchMetadata) Extend(ctx context.Context, ttl time.Duration) error {
114+
func (b *batchMetadata) Extend(ctx context.Context, ttl time.Duration) error {
114115
if !atomic.CompareAndSwapInt32(&b.closed, 0, 0) {
115116
return WithStack(ErrBatchClosed)
116117
}

test/replication_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Ewout Prangsma
21+
// Author Tomasz Mielech
2122
//
2223

2324
package test
2425

2526
import (
2627
"context"
28+
"errors"
2729
"testing"
2830
"time"
2931

32+
"github.com/stretchr/testify/assert"
33+
"github.com/stretchr/testify/require"
34+
3035
driver "github.com/arangodb/go-driver"
3136
)
3237

@@ -85,3 +90,53 @@ func TestReplicationDatabaseInventory(t *testing.T) {
8590
}
8691
}
8792
}
93+
94+
func TestReplicationBatch(t *testing.T) {
95+
ctx := context.Background()
96+
c := createClientFromEnv(t, true)
97+
98+
if _, err := c.Cluster(ctx); err == nil {
99+
// Cluster, not supported for this test
100+
t.Skip("Skipping in cluster")
101+
} else if !driver.IsPreconditionFailed(err) {
102+
t.Errorf("Failed to query cluster: %s", describe(err))
103+
}
104+
105+
rep := c.Replication()
106+
db, err := c.Database(ctx, "_system")
107+
require.NoError(t, err, "failed to open _system database")
108+
109+
var serverID int64 = 1338 // Random test value
110+
batch, err := rep.CreateBatch(ctx, db, serverID, time.Second*60)
111+
require.NoError(t, err, "can not create a batch")
112+
113+
ctxCancel, cancel := context.WithCancel(ctx)
114+
errExtend := make(chan error)
115+
go func(channel chan<- error) {
116+
for {
117+
select {
118+
case <-ctxCancel.Done():
119+
// The batch should be closed.
120+
channel <- batch.Extend(ctx, time.Second*60)
121+
return
122+
default:
123+
// Extend the batch immediately.
124+
if e := batch.Extend(ctx, time.Second*60); e == driver.ErrBatchClosed {
125+
if ctxError := ctx.Err(); ctxError != nil && ctxError != context.Canceled {
126+
channel <- errors.New("the batch extension is closed before it is expected")
127+
return
128+
}
129+
}
130+
}
131+
}
132+
}(errExtend)
133+
134+
// Extend the created batch for 1 second.
135+
time.Sleep(time.Second * 1)
136+
137+
// Delete the batch and interrupt the Go routine for batch extension.
138+
err = batch.Delete(ctx)
139+
cancel()
140+
assert.NoError(t, err, "can not delete a batch")
141+
require.EqualError(t, <-errExtend, driver.ErrBatchClosed.Error(), "batch should be already closed")
142+
}

0 commit comments

Comments
 (0)