-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package graceful | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFinishSync(t *testing.T) { | ||
ctx, _ := TestGraceful(context.Background(), false) | ||
|
||
// call Finish on it and see if we have Sync working | ||
Finish(ctx) | ||
|
||
select { | ||
case <-Sync(ctx): | ||
default: | ||
t.Error("Sync should return a closed channel after Finish call") | ||
} | ||
} | ||
|
||
func TestSyncNoGraceful(t *testing.T) { | ||
// new context with no graceful | ||
ctx := context.Background() | ||
|
||
select { | ||
case <-Sync(ctx): | ||
default: | ||
t.Error("Sync should return closed channel if no graceful exists") | ||
} | ||
} | ||
|
||
func TestGracefulSignal(t *testing.T) { | ||
ctx, g := TestGraceful(context.Background(), false) | ||
|
||
assert.Equal(t, g.Signal, Signal(ctx), | ||
"channel returned by Signal should be equal to g.Signal") | ||
assert.Nil(t, Signal(context.Background()), | ||
"Signal with empty context should return nil") | ||
} | ||
|
||
func TestParentTestParent(t *testing.T) { | ||
ctx, g := TestGraceful(context.Background(), true) | ||
|
||
conn, err := Parent(ctx) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, conn) | ||
assert.Equal(t, g.Parent, conn) | ||
} | ||
|
||
func TestParentNoChild(t *testing.T) { | ||
ctx := context.Background() | ||
ctx, _ = TestGraceful(ctx, false) | ||
|
||
conn, err := Parent(ctx) | ||
assert.Error(t, err) | ||
assert.Nil(t, conn) | ||
} | ||
|
||
func TestParentNoGraceful(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
conn, err := Parent(ctx) | ||
assert.Error(t, err) | ||
assert.Nil(t, conn) | ||
} |