Skip to content

Commit a440021

Browse files
committed
fix(test): fix failed test cases due to race condition
Signed-off-by: Derek Su <[email protected]>
1 parent b228ea7 commit a440021

File tree

3 files changed

+88
-11
lines changed

3 files changed

+88
-11
lines changed

sparse/test/client_test.go

+66-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"os"
77
"testing"
8+
"sync"
89

910
"github.com/stretchr/testify/assert"
1011

@@ -251,11 +252,20 @@ func TestSyncAnyFile(t *testing.T) {
251252

252253
func testSyncAnyFile(t *testing.T, src, dst string, directIO, fastSync bool) {
253254
// Sync
255+
var wg sync.WaitGroup
256+
wg.Add(1)
254257
go func() {
255-
_ = rest.TestServer(context.Background(), port, dst, timeout)
258+
defer wg.Done()
259+
260+
err := rest.TestServer(context.Background(), port, dst, timeout)
261+
// http server is closed by the client after file transfer is done, so the error
262+
// "http: Server closed" is expected.
263+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
256264
}()
257265
err := SyncFile(src, localhost+":"+port, timeout, directIO, fastSync)
258266

267+
wg.Wait()
268+
259269
// Verify
260270
if err != nil {
261271
t.Fatal("sync error")
@@ -271,12 +281,20 @@ func testSyncAnyFile(t *testing.T, src, dst string, directIO, fastSync bool) {
271281

272282
func testSyncAnyFileExpectFailure(t *testing.T, src, dst string, directIO, fastSync bool) {
273283
// Sync
284+
var wg sync.WaitGroup
285+
wg.Add(1)
274286
go func() {
287+
defer wg.Done()
288+
275289
err := rest.TestServer(context.Background(), port, dst, timeout)
276-
assert.Nil(t, err)
290+
// http server is closed by the client after file transfer is done, so the error
291+
// "http: Server closed" is expected.
292+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
277293
}()
278294
err := SyncFile(src, localhost+":"+port, timeout, directIO, fastSync)
279295

296+
wg.Wait()
297+
280298
// Verify
281299
if err == nil {
282300
t.Fatal("sync error")
@@ -733,12 +751,20 @@ func testSyncFile(t *testing.T, layoutLocal, layoutRemote []FileInterval, direct
733751
}
734752

735753
// Sync
754+
var wg sync.WaitGroup
755+
wg.Add(1)
736756
go func() {
757+
defer wg.Done()
758+
737759
err := rest.TestServer(context.Background(), port, remotePath, timeout)
738-
assert.Nil(t, err)
760+
// http server is closed by the client after file transfer is done, so the error
761+
// "http: Server closed" is expected.
762+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
739763
}()
740764
err := SyncFile(localPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)
741765

766+
wg.Wait()
767+
742768
// Verify
743769
if err != nil {
744770
t.Fatal("sync error")
@@ -775,37 +801,61 @@ func Benchmark_1G_InitFiles(b *testing.B) {
775801
}
776802

777803
func Benchmark_1G_SendFiles_Whole(b *testing.B) {
804+
var wg sync.WaitGroup
805+
wg.Add(1)
806+
778807
go func() {
808+
defer wg.Done()
809+
779810
err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
780-
assert.Nil(b, err)
811+
// http server is closed by the client after file transfer is done, so the error
812+
// "http: Server closed" is expected.
813+
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
781814
}()
782815
err := SyncFile(localBigPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)
783816

817+
wg.Wait()
818+
784819
if err != nil {
785820
b.Fatal("sync error")
786821
}
787822
}
788823

789824
func Benchmark_1G_SendFiles_Whole_No_DirectIO(b *testing.B) {
825+
var wg sync.WaitGroup
826+
wg.Add(1)
790827
go func() {
828+
defer wg.Done()
829+
791830
err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
792-
assert.Nil(b, err)
831+
// http server is closed by the client after file transfer is done, so the error
832+
// "http: Server closed" is expected.
833+
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
793834
}()
794835
err := SyncFile(localBigPath, localhost+":"+port, timeout, false /* directIO */, false /* fastSync */)
795836

837+
wg.Wait()
838+
796839
if err != nil {
797840
b.Fatal("sync error")
798841
}
799842
}
800843

801844
func Benchmark_1G_SendFiles_Diff(b *testing.B) {
802-
845+
var wg sync.WaitGroup
846+
wg.Add(1)
803847
go func() {
848+
defer wg.Done()
849+
804850
err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
805-
assert.Nil(b, err)
851+
// http server is closed by the client after file transfer is done, so the error
852+
// "http: Server closed" is expected.
853+
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
806854
}()
807855
err := SyncFile(localBigPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)
808856

857+
wg.Wait()
858+
809859
if err != nil {
810860
b.Fatal("sync error")
811861
}
@@ -867,12 +917,20 @@ func TestSyncSnapshotZeroByte(t *testing.T) {
867917

868918
func testSyncAnyContent(t *testing.T, snapshotName string, dstFileName string, rw ReaderWriterAt, snapshotSize int64) {
869919
// Sync
920+
var wg sync.WaitGroup
921+
wg.Add(1)
870922
go func() {
923+
defer wg.Done()
924+
871925
err := rest.TestServer(context.Background(), port, dstFileName, timeout)
872-
assert.Nil(t, err)
926+
// http server is closed by the client after file transfer is done, so the error
927+
// "http: Server closed" is expected.
928+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
873929
}()
874930
err := SyncContent(snapshotName, rw, snapshotSize, localhost+":"+port, timeout, true, false)
875931

932+
wg.Wait()
933+
876934
// Verify
877935
if err != nil {
878936
t.Fatalf("sync error: %v", err)

sparse/test/fiemap_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"testing"
1010
"time"
11+
"sync"
1112

1213
"github.com/pkg/errors"
1314
log "github.com/sirupsen/logrus"
@@ -54,16 +55,21 @@ func TestFileSync(t *testing.T) {
5455
// defer fileCleanup(dstPath)
5556
log.Info("Syncing file...")
5657
startTime := time.Now()
58+
var wg sync.WaitGroup
59+
wg.Add(1)
5760
go func() {
61+
defer wg.Done()
62+
5863
err := rest.TestServer(context.Background(), port, dstPath, timeout)
59-
assert.Nil(t, err)
64+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
6065
}()
6166
time.Sleep(time.Second)
6267
err := SyncFile(srcPath, localhost+":"+port, timeout, true, false)
6368
if err != nil {
6469
t.Fatalf("sync error: %v", err)
6570
}
6671
log.Infof("Syncing done, size: %v elapsed: %.2fs", testFileSize, time.Since(startTime).Seconds())
72+
wg.Wait()
6773

6874
startTime = time.Now()
6975
log.Info("Checking...")

sparse/test/ssync_test.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212
"time"
13+
"sync"
1314

1415
log "github.com/sirupsen/logrus"
1516
"github.com/stretchr/testify/assert"
@@ -198,17 +199,24 @@ func RandomSync(t *testing.T, size, seed int64, srcPath, dstPath string, dstCrea
198199
}
199200

200201
log.Infof("Syncing with directIO: %v size: %v", directIO, size)
202+
var wg sync.WaitGroup
203+
wg.Add(1)
201204
go func() {
205+
defer wg.Done()
206+
202207
err := rest.TestServer(context.Background(), port, dstPath, timeout)
203-
assert.Nil(t, err)
208+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
204209
}()
210+
205211
startTime := time.Now()
206212
err := SyncFile(srcPath, localhost+":"+port, timeout, directIO, fastSync)
207213
if err != nil {
208214
t.Fatal("sync error")
209215
}
210216
log.Infof("Syncing done, size: %v elapsed: %.2fs", size, time.Since(startTime).Seconds())
211217

218+
wg.Wait()
219+
212220
startTime = time.Now()
213221
err = checkSparseFiles(srcPath, dstPath)
214222
if err != nil {
@@ -264,9 +272,13 @@ func TestSyncCancellation(t *testing.T) {
264272
dstName := tempFilePath(dstPrefix)
265273

266274
ctx, cancelFunc := context.WithCancel(context.Background())
275+
var wg sync.WaitGroup
276+
wg.Add(1)
267277
go func() {
278+
defer wg.Done()
279+
268280
err := rest.TestServer(ctx, port, dstName, timeout)
269-
assert.Nil(t, err)
281+
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
270282
}()
271283

272284
client := http.Client{}
@@ -311,4 +323,5 @@ func TestSyncCancellation(t *testing.T) {
311323
if httpErr == nil || !strings.Contains(httpErr.Error(), "connection refused") {
312324
t.Fatalf("Unexpected error: %v", httpErr)
313325
}
326+
wg.Wait()
314327
}

0 commit comments

Comments
 (0)