Skip to content

Commit 75ad9b4

Browse files
authored
Merge pull request #890 from starius/unit-race
multi: fix race conditions and run unit tests with -race
2 parents d2c08bb + 2f2c5a3 commit 75ad9b4

31 files changed

+202
-197
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ jobs:
101101
go-version: '~${{ env.GO_VERSION }}'
102102

103103
- name: run unit tests
104-
run: make unit
104+
run: make unit-race
105105

106106
- name: run unit test with postgres
107-
run: make unit-postgres
107+
run: make unit-postgres-race

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,22 @@ clean:
9595
# TESTING
9696
# =======
9797

98-
unit:
98+
unit:
9999
@$(call print, "Running unit tests.")
100100
$(UNIT)
101101

102+
unit-race:
103+
@$(call print, "Running unit race tests.")
104+
$(UNIT) -race
105+
102106
unit-postgres:
103107
@$(call print, "Running unit tests with postgres.")
104108
$(UNIT) -tags=test_db_postgres
105109

110+
unit-postgres-race:
111+
@$(call print, "Running unit race tests with postgres.")
112+
$(UNIT) -race -tags=test_db_postgres
113+
106114
# =========
107115
# UTILITIES
108116
# =========

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
1010
github.com/btcsuite/btcwallet v0.16.10-0.20241127094224-93c858b2ad63
1111
github.com/btcsuite/btcwallet/wtxmgr v1.5.4
12-
github.com/coreos/bbolt v1.3.3
1312
github.com/davecgh/go-spew v1.1.1
1413
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
1514
github.com/fortytw2/leaktest v1.3.0
@@ -35,6 +34,7 @@ require (
3534
github.com/ory/dockertest/v3 v3.10.0
3635
github.com/stretchr/testify v1.9.0
3736
github.com/urfave/cli v1.22.9
37+
go.etcd.io/bbolt v1.3.11
3838
golang.org/x/net v0.36.0
3939
google.golang.org/grpc v1.64.1
4040
google.golang.org/protobuf v1.34.2
@@ -164,7 +164,6 @@ require (
164164
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
165165
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
166166
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec // indirect
167-
go.etcd.io/bbolt v1.3.11 // indirect
168167
go.etcd.io/etcd/api/v3 v3.5.12 // indirect
169168
go.etcd.io/etcd/client/pkg/v3 v3.5.12 // indirect
170169
go.etcd.io/etcd/client/v2 v2.305.12 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,6 @@ github.com/cockroachdb/datadriven v1.0.2 h1:H9MtNqVoVhvd9nCBwOyDjUEdZCREqbIdCJD9
732732
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
733733
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
734734
github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM=
735-
github.com/coreos/bbolt v1.3.3 h1:n6AiVyVRKQFNb6mJlwESEvvLoDyiTzXX7ORAUlkeBdY=
736-
github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
737735
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
738736
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
739737
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=

instantout/manager.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ type Manager struct {
4141
}
4242

4343
// NewInstantOutManager creates a new instantout manager.
44-
func NewInstantOutManager(cfg *Config) *Manager {
44+
func NewInstantOutManager(cfg *Config, height int32) *Manager {
4545
return &Manager{
4646
cfg: cfg,
4747
activeInstantOuts: make(map[lntypes.Hash]*FSM),
4848
blockEpochChan: make(chan int32),
49+
currentHeight: height,
4950
}
5051
}
5152

5253
// Run runs the instantout manager.
53-
func (m *Manager) Run(ctx context.Context, initChan chan struct{},
54-
height int32) error {
55-
54+
func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
5655
log.Debugf("Starting instantout manager")
5756
defer func() {
5857
log.Debugf("Stopping instantout manager")
@@ -62,7 +61,6 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
6261
defer cancel()
6362

6463
m.runCtx = runCtx
65-
m.currentHeight = height
6664

6765
err := m.recoverInstantOuts(runCtx)
6866
if err != nil {

instantout/reservation/manager_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package reservation
33
import (
44
"context"
55
"encoding/hex"
6+
"sync/atomic"
67
"testing"
78
"time"
89

@@ -72,13 +73,14 @@ func TestManager(t *testing.T) {
7273
require.NoError(t, err)
7374

7475
// We'll now expect a spend registration.
75-
spendReg := <-testContext.mockLnd.RegisterSpendChannel
76-
require.Equal(t, spendReg.PkScript, pkScript)
76+
var spendReg atomic.Pointer[test.SpendRegistration]
77+
spendReg.Store(<-testContext.mockLnd.RegisterSpendChannel)
78+
require.Equal(t, spendReg.Load().PkScript, pkScript)
7779

7880
go func() {
7981
// We'll expect a second spend registration.
80-
spendReg = <-testContext.mockLnd.RegisterSpendChannel
81-
require.Equal(t, spendReg.PkScript, pkScript)
82+
spendReg.Store(<-testContext.mockLnd.RegisterSpendChannel)
83+
require.Equal(t, spendReg.Load().PkScript, pkScript)
8284
}()
8385

8486
// We'll now try to lock the reservation.
@@ -90,7 +92,7 @@ func TestManager(t *testing.T) {
9092
require.Error(t, err)
9193

9294
testContext.mockLnd.SpendChannel <- &chainntnfs.SpendDetail{
93-
SpentOutPoint: spendReg.Outpoint,
95+
SpentOutPoint: spendReg.Load().Outpoint,
9496
}
9597

9698
// We'll now expect the reservation to be expired.

loopd/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func getTLSConfig(cfg *Config) (*tls.Config, *credentials.TransportCredentials,
423423
// If the certificate expired or it was outdated, delete it and the TLS
424424
// key and generate a new pair.
425425
if time.Now().After(parsedCert.NotAfter) {
426-
log.Info("TLS certificate is expired or outdated, " +
426+
infof("TLS certificate is expired or outdated, " +
427427
"removing old file then generating a new one")
428428

429429
err := os.Remove(cfg.TLSCertPath)
@@ -464,7 +464,7 @@ func loadCertWithCreate(cfg *Config) (tls.Certificate, *x509.Certificate,
464464
if !lnrpc.FileExists(cfg.TLSCertPath) &&
465465
!lnrpc.FileExists(cfg.TLSKeyPath) {
466466

467-
log.Infof("Generating TLS certificates...")
467+
infof("Generating TLS certificates...")
468468
certBytes, keyBytes, err := cert.GenCertPair(
469469
defaultSelfSignedOrganization, cfg.TLSExtraIPs,
470470
cfg.TLSExtraDomains, cfg.TLSDisableAutofill,
@@ -481,7 +481,7 @@ func loadCertWithCreate(cfg *Config) (tls.Certificate, *x509.Certificate,
481481
return tls.Certificate{}, nil, err
482482
}
483483

484-
log.Infof("Done generating TLS certificates")
484+
infof("Done generating TLS certificates")
485485
}
486486

487487
return cert.LoadCert(cfg.TLSCertPath, cfg.TLSKeyPath)

0 commit comments

Comments
 (0)