diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2018-12-05 00:48:33 +0300 |
---|---|---|
committer | Alexander Kiryukhin <alexander@kiryukhin.su> | 2018-12-05 00:48:33 +0300 |
commit | c582001e89c0b81df1b9eb1e8f1857d1c075a805 (patch) | |
tree | 1269ce0169824e967e0eccc83c257f2a2ab9f445 /rutina_test.go | |
parent | 381df5413c2be2a183945aa334ff1690a2cdfb0c (diff) |
Initial commit
Diffstat (limited to 'rutina_test.go')
-rwxr-xr-x | rutina_test.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/rutina_test.go b/rutina_test.go new file mode 100755 index 0000000..cff73c4 --- /dev/null +++ b/rutina_test.go @@ -0,0 +1,89 @@ +package rutina + +import ( + "context" + "testing" + "time" + + "github.com/pkg/errors" +) + +func TestSuccess(t *testing.T) { + r, _ := New() + counter := 0 + f := func(name string, ttl time.Duration) error { + counter++ + <-time.After(ttl) + counter-- + t.Log(name) + return nil + } + r.Go(func(ctx context.Context) error { + return f("one", 1*time.Second) + }) + r.Go(func(ctx context.Context) error { + return f("two", 2*time.Second) + }) + r.Go(func(ctx context.Context) error { + return f("three", 3*time.Second) + }) + if err := r.Wait(); err != nil { + t.Error("Unexpected error", err) + } + if counter == 0 { + t.Log("All routines done") + } else { + t.Error("Not all routines stopped") + } +} + +func TestError(t *testing.T) { + r, _ := New() + f := func(name string, ttl time.Duration) error { + <-time.After(ttl) + t.Log(name) + return errors.New("error from " + name) + } + r.Go(func(ctx context.Context) error { + return f("one", 1*time.Second) + }) + r.Go(func(ctx context.Context) error { + return f("two", 2*time.Second) + }) + r.Go(func(ctx context.Context) error { + return f("three", 3*time.Second) + }) + if err := r.Wait(); err != nil { + if err.Error() != "error from one" { + t.Error("Must be error from first routine") + } + t.Log(err) + } + t.Log("All routines done") +} + +func TestContext(t *testing.T) { + r, _ := New() + cc := false + r.Go(func(ctx context.Context) error { + <-time.After(1 * time.Second) + return nil + }) + r.Go(func(ctx context.Context) error { + select { + case <-ctx.Done(): + cc = true + return nil + case <-time.After(3 * time.Second): + return errors.New("Timeout") + } + }) + if err := r.Wait(); err != nil { + t.Error("Unexpected error", err) + } + if cc { + t.Log("Second routine succesfuly complete by context done") + } else { + t.Error("Routine not completed by context") + } +} |