2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-09 22:15:35 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-29 14:16:36 +00:00
|
|
|
package testcontext_test
|
|
|
|
|
|
|
|
import (
|
2018-11-20 09:32:18 +00:00
|
|
|
"fmt"
|
2018-10-29 14:16:36 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-11-20 09:32:18 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testcontext"
|
2018-10-29 14:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasic(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
ctx.Go(func() error {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Log(ctx.Dir("a", "b", "c"))
|
|
|
|
t.Log(ctx.File("a", "w", "c.txt"))
|
|
|
|
}
|
2018-11-20 09:32:18 +00:00
|
|
|
|
|
|
|
func TestMessage(t *testing.T) {
|
|
|
|
var subtest test
|
|
|
|
|
|
|
|
ctx := testcontext.NewWithTimeout(&subtest, 50*time.Millisecond)
|
|
|
|
ctx.Go(func() error {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
ctx.Cleanup()
|
|
|
|
|
|
|
|
assert.Contains(t, subtest.errors[0], "Test exceeded timeout")
|
|
|
|
assert.Contains(t, subtest.errors[0], "some goroutines are still running")
|
2018-11-27 15:50:40 +00:00
|
|
|
|
|
|
|
assert.Contains(t, subtest.errors[1], "TestMessage")
|
2018-11-20 09:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
2019-06-13 16:09:05 +01:00
|
|
|
logs []string
|
2018-11-20 09:32:18 +00:00
|
|
|
errors []string
|
|
|
|
fatals []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *test) Name() string { return "Example" }
|
|
|
|
func (t *test) Helper() {}
|
|
|
|
|
2019-06-13 16:09:05 +01:00
|
|
|
func (t *test) Log(args ...interface{}) { t.logs = append(t.logs, fmt.Sprint(args...)) }
|
2018-11-20 09:32:18 +00:00
|
|
|
func (t *test) Error(args ...interface{}) { t.errors = append(t.errors, fmt.Sprint(args...)) }
|
|
|
|
func (t *test) Fatal(args ...interface{}) { t.fatals = append(t.fatals, fmt.Sprint(args...)) }
|