mtest: implement NewCtx and Run, and use them in mnet and mhttp's tests
This commit is contained in:
parent
c6e29715b0
commit
f783b62f6f
@ -8,28 +8,18 @@ import (
|
||||
"net/http/httptest"
|
||||
. "testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
||||
)
|
||||
|
||||
func TestMListenAndServe(t *T) {
|
||||
// TODO mtest.NewCtx
|
||||
ctx := mctx.ChildOf(mctx.New(), "test")
|
||||
logger := mlog.From(ctx)
|
||||
logger.SetMaxLevel(mlog.DebugLevel)
|
||||
mlog.CtxSet(ctx, logger)
|
||||
ctx := mtest.NewCtx()
|
||||
|
||||
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
srv := MListenAndServe(ctx, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(rw, r.Body)
|
||||
})
|
||||
|
||||
srv := MListenAndServe(ctx, h)
|
||||
if err := mrun.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}))
|
||||
|
||||
mtest.Run(ctx, t, func() {
|
||||
body := bytes.NewBufferString("HELLO")
|
||||
resp, err := http.Post("http://"+srv.Addr, "text/plain", body)
|
||||
if err != nil {
|
||||
@ -43,10 +33,7 @@ func TestMListenAndServe(t *T) {
|
||||
} else if string(respBody) != "HELLO" {
|
||||
t.Fatalf("unexpected respBody: %q", respBody)
|
||||
}
|
||||
|
||||
if err := mrun.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddXForwardedFor(t *T) {
|
||||
|
@ -6,10 +6,7 @@ import (
|
||||
"net"
|
||||
. "testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
||||
)
|
||||
|
||||
@ -39,19 +36,9 @@ func TestIsReservedIP(t *T) {
|
||||
}
|
||||
|
||||
func TestMListen(t *T) {
|
||||
// TODO mtest.NewCtx
|
||||
ctx := mctx.ChildOf(mctx.New(), "test")
|
||||
logger := mlog.From(ctx)
|
||||
logger.SetMaxLevel(mlog.DebugLevel)
|
||||
mlog.CtxSet(ctx, logger)
|
||||
|
||||
ctx := mtest.NewCtx()
|
||||
l := MListen(ctx, "", "")
|
||||
if err := mcfg.Populate(ctx, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if err := mrun.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mtest.Run(ctx, t, func() {
|
||||
go func() {
|
||||
conn, err := net.Dial("tcp", l.Addr().String())
|
||||
if err != nil {
|
||||
@ -70,8 +57,5 @@ func TestMListen(t *T) {
|
||||
} else if string(b) != "hello world" {
|
||||
t.Fatalf("read %q from conn", b)
|
||||
}
|
||||
|
||||
if err := mrun.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1,2 +1,66 @@
|
||||
// Package mtest implements functionality useful for testing.
|
||||
package mtest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
// NewCtx creates and returns a root Context suitable for testing.
|
||||
func NewCtx() mctx.Context {
|
||||
ctx := mctx.New()
|
||||
mlog.From(ctx).SetMaxLevel(mlog.DebugLevel)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// SetEnv sets the given environment variable on the given Context, such that it
|
||||
// will be used as if it was a real environment variable when the Run function
|
||||
// from this package is called.
|
||||
func SetEnv(ctx mctx.Context, key, val string) {
|
||||
mctx.GetSetMutableValue(ctx, false, ctxKey(0), func(i interface{}) interface{} {
|
||||
m, _ := i.(map[string]string)
|
||||
if m == nil {
|
||||
m = map[string]string{}
|
||||
}
|
||||
m[key] = val
|
||||
return m
|
||||
})
|
||||
}
|
||||
|
||||
// Run performs the following using the given Context:
|
||||
//
|
||||
// - Calls mcfg.Populate using any variables set by SetEnv.
|
||||
//
|
||||
// - Calls mrun.Start
|
||||
//
|
||||
// - Calls the passed in body callback.
|
||||
//
|
||||
// - Calls mrun.Stop
|
||||
//
|
||||
// The intention is that Run is used within a test on a Context created via
|
||||
// NewCtx, after any setup functions have been called (e.g. mnet.MListen).
|
||||
func Run(ctx mctx.Context, t *testing.T, body func()) {
|
||||
envMap, _ := mctx.MutableValue(ctx, ctxKey(0)).(map[string]string)
|
||||
env := make([]string, 0, len(envMap))
|
||||
for key, val := range envMap {
|
||||
env = append(env, key+"="+val)
|
||||
}
|
||||
|
||||
if err := mcfg.Populate(ctx, mcfg.SourceEnv{Env: env}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if err := mrun.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body()
|
||||
|
||||
if err := mrun.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
18
mtest/mtest_test.go
Normal file
18
mtest/mtest_test.go
Normal file
@ -0,0 +1,18 @@
|
||||
package mtest
|
||||
|
||||
import (
|
||||
. "testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||
)
|
||||
|
||||
func TestRun(t *T) {
|
||||
ctx := NewCtx()
|
||||
arg := mcfg.RequiredString(ctx, "arg", "Required by this test")
|
||||
SetEnv(ctx, "ARG", "foo")
|
||||
Run(ctx, t, func() {
|
||||
if *arg != "foo" {
|
||||
t.Fatalf(`arg not set to "foo", is set to %q`, *arg)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user