storj/private/testcontext/context_test.go

57 lines
1.2 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
2018-10-29 14:16:36 +00:00
package testcontext_test
import (
"fmt"
2018-10-29 14:16:36 +00:00
"testing"
"time"
"github.com/stretchr/testify/assert"
"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"))
}
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")
assert.Contains(t, subtest.errors[1], "TestMessage")
}
type test struct {
logs []string
errors []string
fatals []string
}
func (t *test) Name() string { return "Example" }
func (t *test) Helper() {}
func (t *test) Log(args ...interface{}) { t.logs = append(t.logs, fmt.Sprint(args...)) }
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...)) }