Skip to content

Commit

Permalink
fix race condition + fix all go linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik committed May 29, 2024
1 parent 5181ae8 commit 5bab72c
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/networkservice/common/mechanisms/recvfd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -233,12 +235,16 @@ func TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin(t *testing.T) {
goleak.VerifyNone(t)
})

eventFactoryCh := make(chan begin.EventFactory, 1)
var once sync.Once
// Create a server
var eventFactory begin.EventFactory
server := chain.NewNetworkServiceServer(
begin.NewServer(),
checkcontextonreturn.NewServer(t, func(t *testing.T, ctx context.Context) {
eventFactory = begin.FromContext(ctx)
once.Do(func() {
eventFactoryCh <- begin.FromContext(ctx)
close(eventFactoryCh)
})
}),
recvfd.NewServer(),
injecterror.NewServer(
Expand All @@ -248,7 +254,7 @@ func TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin(t *testing.T) {
)

tempDir := t.TempDir()
sock, err := os.Create(path.Join(tempDir, "test.sock"))
sock, err := os.Create(filepath.Clean(path.Join(tempDir, "test.sock")))
require.NoError(t, err)

serveURL := &url.URL{Scheme: "unix", Path: sock.Name()}
Expand All @@ -261,7 +267,7 @@ func TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin(t *testing.T) {
c := createClient(ctx, serveURL)

// Create a file to send
testFileName := path.Join(tempDir, "TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin.test")
testFileName := filepath.Clean(path.Join(tempDir, "TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin.test"))
f, err := os.Create(testFileName)
require.NoErrorf(t, err, "Failed to create and open a file: %v", err)
err = f.Close()
Expand Down Expand Up @@ -293,6 +299,7 @@ func TestRecvfdDoesntWaitForAnyFilesOnRequestsFromBegin(t *testing.T) {

// Send Close. Recvfd shouldn't freeze trying to read files
// from the client because we send Close from begin.
eventFactory := <-eventFactoryCh
ch := eventFactory.Close()
err = <-ch
require.NoError(t, err)
Expand Down

0 comments on commit 5bab72c

Please sign in to comment.