Skip to content

Commit f2fb722

Browse files
committed
reservation: add initchan
This fixes a flake in the unit tests
1 parent 246becb commit f2fb722

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

instantout/reservation/manager.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func NewManager(cfg *Config) *Manager {
3434
}
3535

3636
// Run runs the reservation manager.
37-
func (m *Manager) Run(ctx context.Context, height int32) error {
37+
func (m *Manager) Run(ctx context.Context, height int32,
38+
initChan chan struct{}) error {
39+
3840
log.Debugf("Starting reservation manager")
3941

4042
runCtx, cancel := context.WithCancel(ctx)
@@ -55,6 +57,9 @@ func (m *Manager) Run(ctx context.Context, height int32) error {
5557

5658
ntfnChan := m.cfg.NotificationManager.SubscribeReservations(runCtx)
5759

60+
// Signal that the manager has been initialized.
61+
close(initChan)
62+
5863
for {
5964
select {
6065
case height := <-newBlockChan:

instantout/reservation/manager_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ func TestManager(t *testing.T) {
2525

2626
testContext := newManagerTestContext(t)
2727

28+
initChan := make(chan struct{})
2829
// Start the manager.
2930
go func() {
30-
err := testContext.manager.Run(ctxb, testContext.mockLnd.Height)
31+
err := testContext.manager.Run(ctxb, testContext.mockLnd.Height, initChan)
3132
require.NoError(t, err)
3233
}()
3334

35+
// We'll now wait for the manager to be initialized.
36+
<-initChan
37+
3438
// Create a new reservation.
3539
reservationFSM, err := testContext.manager.newReservation(
3640
ctxb, uint32(testContext.mockLnd.Height),

loopd/daemon.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
648648
// Start the reservation manager.
649649
if d.reservationManager != nil {
650650
d.wg.Add(1)
651+
initChan := make(chan struct{})
651652
go func() {
652653
defer d.wg.Done()
653654

@@ -663,12 +664,25 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
663664
defer log.Info("Reservation manager stopped")
664665

665666
err = d.reservationManager.Run(
666-
d.mainCtx, int32(getInfo.BlockHeight),
667+
d.mainCtx, int32(getInfo.BlockHeight), initChan,
667668
)
668669
if err != nil && !errors.Is(err, context.Canceled) {
669670
d.internalErrChan <- err
670671
}
671672
}()
673+
674+
// Wait for the reservation server to be ready before starting the
675+
// grpc server.
676+
timeOutCtx, cancel := context.WithTimeout(d.mainCtx, 10*time.Second)
677+
select {
678+
case <-timeOutCtx.Done():
679+
cancel()
680+
return fmt.Errorf("reservation server not ready: %v",
681+
timeOutCtx.Err())
682+
683+
case <-initChan:
684+
cancel()
685+
}
672686
}
673687

674688
// Start the instant out manager.
@@ -701,8 +715,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
701715
select {
702716
case <-timeOutCtx.Done():
703717
cancel()
704-
return fmt.Errorf("reservation server not ready: %v",
718+
return fmt.Errorf("instantout server not ready: %v",
705719
timeOutCtx.Err())
720+
706721
case <-initChan:
707722
cancel()
708723
}

0 commit comments

Comments
 (0)