Skip to content

Commit

Permalink
Let the daemon advise when ready
Browse files Browse the repository at this point in the history
The deamon knows best when is the most likely moment to
be serving.
  • Loading branch information
CarlosNihelton committed Nov 26, 2024
1 parent 45193ca commit d0d0fbf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions windows-agent/internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ var defaultOptions = options{
netMonitoringProvider: netmonitoring.DefaultAPIProvider,
}

// WaitReady blocks until the daemon is ready to serve, i.e. until Serve has been called.
func (d *Daemon) WaitReady() {
<-d.serving
}

// Option represents an optional function to override getWslIP default values.
type Option func(*options)

Expand Down
58 changes: 58 additions & 0 deletions windows-agent/internal/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,64 @@ func TestQuitBeforeServe(t *testing.T) {
daemontestutils.RequireWaitPathDoesNotExist(t, filepath.Join(addrDir, common.ListeningPortFileName), "Port file should not exist after returning from Serve()")
}

func TestWaitReady(t *testing.T) {
t.Parallel()

registerer := func(context.Context, bool) *grpc.Server {
return grpc.NewServer()
}

testcases := map[string]struct {
skipServe bool

wantBlock bool
}{
"Success when serving": {},

"Block when not serving": {skipServe: true, wantBlock: true},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addrDir := t.TempDir()

d := daemon.New(ctx, registerer, addrDir)
serverErr := make(chan error)
if !tc.skipServe {
go func() {
defer close(serverErr)
serverErr <- d.Serve(ctx)
}()
}

ready := make(chan struct{})
go func() {
// This would block until the server is ready, potentially "forever" if the server never starts.
d.WaitReady()
close(ready)
}()

select {
case err := <-serverErr:
require.Fail(t, "Serve error", err)
case <-ctx.Done():
require.Fail(t, "Context should not be cancelled yet")
case <-ready:
if tc.wantBlock {
require.Fail(t, "WaitReady should not return as the server should not ready")
}
case <-time.After(1 * time.Second):
if !tc.wantBlock {
require.Fail(t, "WaitReady should block forever in this case")
}
}

})
}
}

// grpcPersistentCall will create a persistent GRPC connection to the server.
// It will return immediately. drop() should be called to ends the connection from
// the client side. It returns the GRPC error code if any.
Expand Down

0 comments on commit d0d0fbf

Please sign in to comment.