Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): fix failed test cases due to race condition #105

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .drone.yml

This file was deleted.

16 changes: 5 additions & 11 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
FROM ubuntu:16.04
# FROM arm=armhf/ubuntu:16.04
FROM registry.suse.com/bci/golang:1.22

ARG DAPPER_HOST_ARCH=amd64
ARG http_proxy
ARG https_proxy
ENV HOST_ARCH=${DAPPER_HOST_ARCH} ARCH=${DAPPER_HOST_ARCH}

RUN apt-get update && \
apt-get install -y gcc ca-certificates git wget curl vim less file && \
rm -f /bin/sh && ln -s /bin/bash /bin/sh
RUN zypper -n addrepo --refresh https://download.opensuse.org/repositories/Base:System/openSUSE_Factory/Base:System.repo && \
zypper --gpg-auto-import-keys ref

ENV DOCKER_URL_amd64=https://get.docker.com/builds/Linux/x86_64/docker-1.10.3 \
DOCKER_URL_arm64=https://github.com/rancher/docker/releases/download/v1.10.3-ros1/docker-1.10.3_arm \
DOCKER_URL=DOCKER_URL_${ARCH}

RUN wget -O - ${!DOCKER_URL} > /usr/bin/docker && chmod +x /usr/bin/docker
RUN zypper -n install glibc glibc-static gcc ca-certificates git wget curl vim less file awk docker && \
rm -rf /var/cache/zypp/*

ENV GOLANG_ARCH_amd64=amd64 GOLANG_ARCH_arm64=arm64 GOLANG_ARCH=GOLANG_ARCH_${ARCH} \
GOPATH=/go PATH=/go/bin:/usr/local/go/bin:${PATH} SHELL=/bin/bash

RUN wget -O - https://storage.googleapis.com/golang/go1.22.2.linux-${!GOLANG_ARCH}.tar.gz | tar -xzf - -C /usr/local
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2

ENV DAPPER_SOURCE /go/src/github.com/longhorn/sparse-tools
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/longhorn/sparse-tools

go 1.22
go 1.22.0

require (
github.com/google/uuid v1.6.0
Expand Down
2 changes: 1 addition & 1 deletion scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ echo Running tests

PACKAGES="$(find -name '*.go' | xargs -I{} dirname {} | cut -f2 -d/ | sort -u | grep -Ev '(^\.$|.git|.trash-cache|vendor|bin)' | sed -e 's!^!./!' -e 's!$!/...!')"

go test -race -cover -tags=test ${PACKAGES} -coverprofile=coverage.out
go test -v -race -cover -tags=test ${PACKAGES} -coverprofile=coverage.out
74 changes: 66 additions & 8 deletions sparse/test/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"os"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -251,11 +252,20 @@ func TestSyncAnyFile(t *testing.T) {

func testSyncAnyFile(t *testing.T, src, dst string, directIO, fastSync bool) {
// Sync
var wg sync.WaitGroup
wg.Add(1)
go func() {
_ = rest.TestServer(context.Background(), port, dst, timeout)
defer wg.Done()

err := rest.TestServer(context.Background(), port, dst, timeout)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(src, localhost+":"+port, timeout, directIO, fastSync)

wg.Wait()

// Verify
if err != nil {
t.Fatal("sync error")
Expand All @@ -271,12 +281,20 @@ func testSyncAnyFile(t *testing.T, src, dst string, directIO, fastSync bool) {

func testSyncAnyFileExpectFailure(t *testing.T, src, dst string, directIO, fastSync bool) {
// Sync
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, dst, timeout)
assert.Nil(t, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(src, localhost+":"+port, timeout, directIO, fastSync)

wg.Wait()

// Verify
if err == nil {
t.Fatal("sync error")
Expand Down Expand Up @@ -733,12 +751,20 @@ func testSyncFile(t *testing.T, layoutLocal, layoutRemote []FileInterval, direct
}

// Sync
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, remotePath, timeout)
assert.Nil(t, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(localPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)

wg.Wait()

// Verify
if err != nil {
t.Fatal("sync error")
Expand Down Expand Up @@ -775,37 +801,61 @@ func Benchmark_1G_InitFiles(b *testing.B) {
}

func Benchmark_1G_SendFiles_Whole(b *testing.B) {
var wg sync.WaitGroup
wg.Add(1)

go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
assert.Nil(b, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(localBigPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)

wg.Wait()

if err != nil {
b.Fatal("sync error")
}
}

func Benchmark_1G_SendFiles_Whole_No_DirectIO(b *testing.B) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
assert.Nil(b, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(localBigPath, localhost+":"+port, timeout, false /* directIO */, false /* fastSync */)

wg.Wait()

if err != nil {
b.Fatal("sync error")
}
}

func Benchmark_1G_SendFiles_Diff(b *testing.B) {

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, remoteBigPath, timeout)
assert.Nil(b, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(b, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncFile(localBigPath, localhost+":"+port, timeout, true /* directIO */, false /* fastSync */)

wg.Wait()

if err != nil {
b.Fatal("sync error")
}
Expand Down Expand Up @@ -867,12 +917,20 @@ func TestSyncSnapshotZeroByte(t *testing.T) {

func testSyncAnyContent(t *testing.T, snapshotName string, dstFileName string, rw ReaderWriterAt, snapshotSize int64) {
// Sync
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, dstFileName, timeout)
assert.Nil(t, err)
// http server is closed by the client after file transfer is done, so the error
// "http: Server closed" is expected.
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
err := SyncContent(snapshotName, rw, snapshotSize, localhost+":"+port, timeout, true, false)

wg.Wait()

// Verify
if err != nil {
t.Fatalf("sync error: %v", err)
Expand Down
8 changes: 7 additions & 1 deletion sparse/test/fiemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -54,16 +55,21 @@ func TestFileSync(t *testing.T) {
// defer fileCleanup(dstPath)
log.Info("Syncing file...")
startTime := time.Now()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, dstPath, timeout)
assert.Nil(t, err)
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()
time.Sleep(time.Second)
err := SyncFile(srcPath, localhost+":"+port, timeout, true, false)
if err != nil {
t.Fatalf("sync error: %v", err)
}
log.Infof("Syncing done, size: %v elapsed: %.2fs", testFileSize, time.Since(startTime).Seconds())
wg.Wait()

startTime = time.Now()
log.Info("Checking...")
Expand Down
17 changes: 15 additions & 2 deletions sparse/test/ssync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -198,17 +199,24 @@ func RandomSync(t *testing.T, size, seed int64, srcPath, dstPath string, dstCrea
}

log.Infof("Syncing with directIO: %v size: %v", directIO, size)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(context.Background(), port, dstPath, timeout)
assert.Nil(t, err)
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()

startTime := time.Now()
err := SyncFile(srcPath, localhost+":"+port, timeout, directIO, fastSync)
if err != nil {
t.Fatal("sync error")
}
log.Infof("Syncing done, size: %v elapsed: %.2fs", size, time.Since(startTime).Seconds())

wg.Wait()

startTime = time.Now()
err = checkSparseFiles(srcPath, dstPath)
if err != nil {
Expand Down Expand Up @@ -264,9 +272,13 @@ func TestSyncCancellation(t *testing.T) {
dstName := tempFilePath(dstPrefix)

ctx, cancelFunc := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

err := rest.TestServer(ctx, port, dstName, timeout)
assert.Nil(t, err)
assert.True(t, err == nil || err.Error() == "http: Server closed", "Unexpected error: %v", err)
}()

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