Skip to content

Commit

Permalink
Update tests for Runner.State
Browse files Browse the repository at this point in the history
  • Loading branch information
tep committed Aug 4, 2021
1 parent dcd4bd1 commit 615016a
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ func (tc *testcase) test(t *testing.T) {

var wg sync.WaitGroup

r, err := NewRunner(svr, tc.runOptions...)
if err != nil {
t.Fatalf("cannot create Runner: %v", err)
}

tc.chkState(t, r, NotStarted)

wg.Add(1)

go func() {
defer wg.Done()
defer close(errs)

if err := Run(ctx, svr, tc.runOptions...); err != nil {
if err := r.Run(ctx); err != nil {
tl.Infof("Run error: %v", err)
errs <- err
} else {
Expand Down Expand Up @@ -111,11 +118,17 @@ func (tc *testcase) test(t *testing.T) {
})
}

r.Ready()

tc.chkState(t, r, Running)

if got := <-errs; !tc.isWanted(got) {
t.Errorf("Run(ctx, svr) == (%v); wanted (%v)", got, tc.want)
}

wg.Wait()

tc.chkState(t, r, Stopped)
}

func (tc *testcase) isWanted(got error) bool {
Expand All @@ -126,6 +139,30 @@ func (tc *testcase) isWanted(got error) bool {
return got == tc.want
}

func (tc *testcase) chkState(t *testing.T, r *Runner, want State) {
t.Helper()

t.Logf("want:%v tc:%#v", want, tc)

switch want {
case Stopped:
if tc.want == context.Canceled {
want = Failed
}
fallthrough

case Running:
time.Sleep(100 * time.Microsecond) // to compensate for test flakiness
if tc.serveError == errServeFailed {
want = Failed
}
}

if got := r.State(); got != want {
t.Errorf("r.State() == %v; wanted %v", got, want)
}
}

// A convenience method added to type LameDuckError (only during testing).
func (lde *LameDuckError) isEqual(err error) bool {
switch {
Expand Down

0 comments on commit 615016a

Please sign in to comment.