Skip to content

Commit

Permalink
Add workaround for captive code bug in integration tests
Browse files Browse the repository at this point in the history
Add a workaround for stellar/go#5342 ,
which is causing a race in integration tests.
  • Loading branch information
2opremio committed Jun 18, 2024
1 parent 3d2810f commit 719110f
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (i *Test) spawnContainers() {
i.runSuccessfulComposeCommand(upCmd...)
if i.runRPCInContainer() {
i.rpcContainerLogsCommand = i.getComposeCommand("logs", "--no-log-prefix", "-f", "rpc")
writer := testLogWriter{t: i.t, prefix: fmt.Sprintf(`rpc="container" version="%s" `, i.rpcContainerVersion)}
writer := newTestLogWriter(i.t, fmt.Sprintf(`rpc="container" version="%s" `, i.rpcContainerVersion))
i.rpcContainerLogsCommand.Stdout = writer
i.rpcContainerLogsCommand.Stderr = writer
require.NoError(i.t, i.rpcContainerLogsCommand.Start())
Expand Down Expand Up @@ -397,12 +397,34 @@ func (i *Test) generateRPCConfigFile(rpcConfig rpcConfig) {
require.NoError(i.t, err)
}

func newTestLogWriter(t *testing.T, prefix string) *testLogWriter {
tw := &testLogWriter{t: t, prefix: prefix}
t.Cleanup(func() {
tw.testDoneMx.Lock()
tw.testDone = true
tw.testDoneMx.Lock()
})
return tw
}

type testLogWriter struct {
t *testing.T
prefix string
// TODO: remove once the tickets are fixed
testDoneMx sync.RWMutex
testDone bool
}

func (tw testLogWriter) Write(p []byte) (n int, err error) {
func (tw *testLogWriter) Write(p []byte) (n int, err error) {
tw.testDoneMx.RLock()
if tw.testDone {
// Workaround for https://github.com/stellar/go/issues/5342
// and https://github.com/stellar/go/issues/5350, which causes a race condition
// in test logging
tw.testDoneMx.RUnlock()
return len(p), nil
}
tw.testDoneMx.RUnlock()
all := strings.TrimSpace(string(p))
lines := strings.Split(all, "\n")
for _, l := range lines {
Expand All @@ -423,7 +445,7 @@ func (i *Test) createRPCDaemon(c rpcConfig) *daemon.Daemon {
cfg.HistoryArchiveUserAgent = fmt.Sprintf("soroban-rpc/%s", config.Version)

logger := supportlog.New()
logger.SetOutput(testLogWriter{t: i.t, prefix: `rpc="daemon" `})
logger.SetOutput(newTestLogWriter(i.t, `rpc="daemon" `))
return daemon.MustNew(&cfg, logger)
}

Expand Down

0 comments on commit 719110f

Please sign in to comment.