Skip to content

Commit

Permalink
fix: avoid flaky test race
Browse files Browse the repository at this point in the history
Occasionally the mock matcher will complain about the fake childCtx, so
ignore that since it is an implementation detail.
  • Loading branch information
smlx committed Feb 20, 2024
1 parent a6fc4f6 commit 683d965
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
10 changes: 8 additions & 2 deletions internal/sshserver/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ var ParseLogsArg = parseLogsArg
// SessionHandler exposes the private sessionHandler for testing only.
var SessionHandler = sessionHandler

// CtxKey exposes the private ctxKey for testing only.
type CtxKey = ctxKey
// Exposes the private ctxKey constants for testing only.
const (
EnvironmentIDKey = environmentIDKey
EnvironmentNameKey = environmentNameKey
ProjectIDKey = projectIDKey
ProjectNameKey = projectNameKey
SSHFingerprint = sshFingerprint
)
29 changes: 13 additions & 16 deletions internal/sshserver/sessionhandler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sshserver_test

import (
"context"
"log/slog"
"os"
"testing"
Expand Down Expand Up @@ -67,11 +66,11 @@ func TestExec(t *testing.T) {
tc.user,
tc.deployment,
).Return(tc.deployment, nil)
sshContext.EXPECT().Value(sshserver.CtxKey(0)).Return(0)
sshContext.EXPECT().Value(sshserver.CtxKey(1)).Return("test")
sshContext.EXPECT().Value(sshserver.CtxKey(2)).Return(0)
sshContext.EXPECT().Value(sshserver.CtxKey(3)).Return("project")
sshContext.EXPECT().Value(sshserver.CtxKey(4)).Return("fingerprint")
sshContext.EXPECT().Value(sshserver.EnvironmentIDKey).Return(0)
sshContext.EXPECT().Value(sshserver.EnvironmentNameKey).Return("test")
sshContext.EXPECT().Value(sshserver.ProjectIDKey).Return(0)
sshContext.EXPECT().Value(sshserver.ProjectNameKey).Return("project")
sshContext.EXPECT().Value(sshserver.SSHFingerprint).Return("fingerprint")
winch := make(<-chan ssh.Window)
sshSession.EXPECT().Pty().Return(ssh.Pty{}, winch, tc.pty)
sshSession.EXPECT().Stderr().Return(os.Stderr)
Expand Down Expand Up @@ -142,20 +141,18 @@ func TestLogs(t *testing.T) {
tc.user,
tc.deployment,
).Return(tc.deployment, nil)
sshContext.EXPECT().Value(sshserver.CtxKey(0)).Return(0)
sshContext.EXPECT().Value(sshserver.CtxKey(1)).Return("test")
sshContext.EXPECT().Value(sshserver.CtxKey(2)).Return(0)
sshContext.EXPECT().Value(sshserver.CtxKey(3)).Return("project")
sshContext.EXPECT().Value(sshserver.CtxKey(4)).Return("fingerprint")
sshContext.EXPECT().Value(sshserver.EnvironmentIDKey).Return(0)
sshContext.EXPECT().Value(sshserver.EnvironmentNameKey).Return("test")
sshContext.EXPECT().Value(sshserver.ProjectIDKey).Return(0)
sshContext.EXPECT().Value(sshserver.ProjectNameKey).Return("project")
sshContext.EXPECT().Value(sshserver.SSHFingerprint).Return("fingerprint")

// this call is executed by context.WithCancel()
sshContext.EXPECT().Value(gomock.Any()).Return(nil).Times(4)
// called by context.WithCancel()
sshContext.EXPECT().Value(gomock.Any()).Return(nil).AnyTimes()

sshContext.EXPECT().Done().Return(make(<-chan struct{})).AnyTimes()
childCtx, cancel := context.WithCancel(sshContext)
defer cancel()
k8sService.EXPECT().Logs(
childCtx,
gomock.Any(), // private childCtx
tc.user,
tc.deployment,
"",
Expand Down

0 comments on commit 683d965

Please sign in to comment.