Skip to content

Commit

Permalink
Fix flaky test. (#289)
Browse files Browse the repository at this point in the history
* Fix flaky test.

- Don't try to release a semaphore if acquire errors, as the error might be context deadline exceeded in a test.
- Squelch error closing serverStdin if the error is that it's already closed.
- Make timeout in passingvalues_test 0 so that context is always deadline exceeded, preventing flakes and preventing entrypoint() from running forever.

* Better error check.
  • Loading branch information
ggreer authored Jan 16, 2025
1 parent 0e762a1 commit 4d0593a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (cw *wrapper) Close() error {

if cw.serverStdin != nil {
err = cw.serverStdin.Close()
if err != nil {
if err != nil && errors.Is(err, os.ErrClosed) {
return fmt.Errorf("error closing connector service stdin: %w", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tests/notrequireargsinsubcommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestCallSubCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), timeoutIn)
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()

requiredField := field.StringField("name", field.WithRequired(true))
Expand Down
6 changes: 2 additions & 4 deletions internal/tests/passingvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package tests
import (
"context"
"testing"
"time"

"github.com/conductorone/baton-sdk/pkg/field"
"github.com/stretchr/testify/require"
)

const timeoutIn = time.Microsecond * 1

func TestEntryPoint(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), timeoutIn)
// We want a context that is already DeadlineExceeded so that entrypoint() doesn't hang
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()

stringRequiredField := field.StringField(
Expand Down
7 changes: 5 additions & 2 deletions pkg/connectorrunner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ func (c *connectorRunner) run(ctx context.Context) error {
// Acquire a worker slot before we call Next() so we don't claim a task before we can actually process it.
err = sem.Acquire(ctx, 1)
if err != nil {
// Any error returned from Acquire() is due to the context being cancelled.
sem.Release(1)
if errors.Is(err, context.Canceled) {
// Any error returned from Acquire() is due to the context being cancelled.
// Except for some tests where error is context deadline exceeded
sem.Release(1)
}
return c.handleContextCancel(ctx)
}
l.Debug("runner: worker claimed, checking for next task")
Expand Down

0 comments on commit 4d0593a

Please sign in to comment.