Esempio n. 1
0
func TestChannelServerClose(t *testing.T) {
	sm, cm := newServerAndClient()
	wg := &sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		c, err := sm.Accept()
		assert.NoError(t, err)
		if err != nil {
			return
		}
		b := make([]byte, 4)
		_, err = c.Read(b)
		assert.NoError(t, err)
		err = c.Close()
		assert.NoError(t, err)
	}()

	c, err := cm.Dial()
	assert.NoError(t, err)
	_, err = c.Write([]byte("PING"))
	assert.NoError(t, err)
	b := make([]byte, 4)
	_, err = c.Read(b)
	assert.Equal(t, io.EOF, err)

	wg.Wait()
}
Esempio n. 2
0
func TestBinaryEncodeComplex(t *testing.T) {
	b, err := Marshal(s1v)
	assert.NoError(t, err)
	assert.Equal(t, svb, b)
	s := &s1{}
	err = Unmarshal(svb, s)
	assert.NoError(t, err)
	assert.Equal(t, s1v, s)
}
Esempio n. 3
0
func TestParseTCPAddrList(t *testing.T) {
	p := parserMixin{}
	v := p.TCPList()
	err := p.value.Set("127.0.0.1:1234")
	assert.NoError(t, err)
	err = p.value.Set("127.0.0.1:1235")
	assert.NoError(t, err)
	assert.Equal(t, "127.0.0.1:1234,127.0.0.1:1235", (*tcpAddrsValue)(v).String())
}
Esempio n. 4
0
func TestParseTCPAddr(t *testing.T) {
	p := parserMixin{}
	v := p.TCP()
	err := p.value.Set("127.0.0.1:1234")
	assert.NoError(t, err)
	expected, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234")
	assert.NoError(t, err)
	assert.Equal(t, *expected, **v)
}
Esempio n. 5
0
func TestRequiredFlag(t *testing.T) {
	fg := newFlagGroup()
	fg.Flag("a", "").Required().Bool()
	assert.NoError(t, fg.init())
	tokens := Tokenize([]string{"--a"})
	tokens, err := fg.parse(tokens, false)
	assert.NoError(t, err)
	tokens = Tokenize([]string{})
	tokens, err = fg.parse(tokens, false)
	assert.Error(t, err)
}
Esempio n. 6
0
func TestTooManyArgs(t *testing.T) {
	a := New("test", "test")
	a.Arg("a", "").String()
	assert.NoError(t, a.init())
	toks := Tokenize([]string{"a", "b"})
	toks, _, err := a.parse(toks)
	assert.NoError(t, err)
	assert.Equal(t, tokens{&token{TokenArg, "b"}}, toks)
	_, err = a.Parse([]string{"a", "b"})
	assert.Error(t, err)
}
Esempio n. 7
0
func TestTooManyArgs(t *testing.T) {
	a := New("test", "test")
	a.Arg("a", "").String()
	assert.NoError(t, a.init())
	context := Tokenize([]string{"a", "b"})
	_, err := a.parse(context)
	assert.NoError(t, err)
	assert.Equal(t, Tokens{&Token{TokenArg, "b"}}, context.Tokens)
	_, err = a.Parse([]string{"a", "b"})
	assert.Error(t, err)
}
Esempio n. 8
0
func TestParseMetricBytes(t *testing.T) {
	n, err := ParseMetricBytes("0B")
	assert.NoError(t, err)
	assert.Equal(t, 0, n)
	n, err = ParseMetricBytes("1KB1B")
	assert.NoError(t, err)
	assert.Equal(t, 1001, n)
	n, err = ParseMetricBytes("1MB1KB25B")
	assert.NoError(t, err)
	assert.Equal(t, 1001025, n)
	n, err = ParseMetricBytes("1.5MB")
	assert.NoError(t, err)
	assert.Equal(t, 1500000, n)
}
Esempio n. 9
0
func TestParseBase2Bytes(t *testing.T) {
	n, err := ParseBase2Bytes("0B")
	assert.NoError(t, err)
	assert.Equal(t, 0, n)
	n, err = ParseBase2Bytes("1KB")
	assert.NoError(t, err)
	assert.Equal(t, 1024, n)
	n, err = ParseBase2Bytes("1MB1KB25B")
	assert.NoError(t, err)
	assert.Equal(t, 1049625, n)
	n, err = ParseBase2Bytes("1.5MB")
	assert.NoError(t, err)
	assert.Equal(t, 1572864, n)
}
Esempio n. 10
0
func TestCommander(t *testing.T) {
	c := New("test", "test")
	ping := c.Command("ping", "Ping an IP address.")
	pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration()

	selected, err := c.Parse([]string{"ping"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 5*time.Second, *pingTTL)

	selected, err = c.Parse([]string{"ping", "--ttl=10s"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 10*time.Second, *pingTTL)
}
Esempio n. 11
0
func TestUnmarshalStringField(t *testing.T) {
	mE := MustNewMapping(&everythingComp{})
	var s string
	err := mE.UnmarshalField("Key", []byte("keyhere"), &s)
	assert.NoError(t, err)
	assert.Equal(t, s, "keyhere")
}
Esempio n. 12
0
func TestMarshalUnMarshalTypeAliases(t *testing.T) {
	type Foo int64
	f := Foo(32)
	b, err := Marshal(f)
	assert.NoError(t, err)
	assert.Equal(t, []byte{0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, b)
}
Esempio n. 13
0
func TestTopLevelArgWorks(t *testing.T) {
	c := New("test", "test")
	s := c.Arg("arg", "help").String()
	_, err := c.Parse([]string{"foo"})
	assert.NoError(t, err)
	assert.Equal(t, "foo", *s)
}
Esempio n. 14
0
func TestUnmarshalIntField(t *testing.T) {
	mE := MustNewMapping(&tagsA{})
	var i int
	err := mE.UnmarshalField("B", []byte{0, 0, 0, 0, 0, 0, 0, 1}, &i)
	assert.NoError(t, err)
	assert.Equal(t, i, 1)
}
Esempio n. 15
0
func TestParseURL(t *testing.T) {
	p := parserMixin{}
	v := p.URL()
	p.value.Set("http://w3.org")
	u, err := url.Parse("http://w3.org")
	assert.NoError(t, err)
	assert.Equal(t, *u, **v)
}
Esempio n. 16
0
func TestInvalidArgsDefaultCanBeOverridden(t *testing.T) {
	a := newArgGroup()
	a.Arg("a", "").Default("invalid").Bool()
	assert.NoError(t, a.init())
	tokens := Tokenize([]string{})
	_, err := a.parse(tokens)
	assert.Error(t, err)
}
Esempio n. 17
0
func TestInvalidFlagDefaultCanBeOverridden(t *testing.T) {
	fg := newFlagGroup()
	f := fg.Flag("a", "").Default("invalid")
	f.Bool()
	assert.NoError(t, fg.init())
	tokens := Tokenize([]string{})
	_, err := fg.parse(tokens, false)
	assert.Error(t, err)
}
Esempio n. 18
0
func TestParseExistingFile(t *testing.T) {
	p := parserMixin{}
	v := p.ExistingFile()
	err := p.value.Set("/etc/hosts")
	assert.NoError(t, err)
	assert.Equal(t, "/etc/hosts", *v)
	err = p.value.Set("/etc/hostsDEFINITELYMISSING")
	assert.Error(t, err)
}
Esempio n. 19
0
func TestTupleSpaceTake(t *testing.T) {
	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())
	defer ts.Shutdown()
	ts.Send(tuplespace.Tuple{"cmd": "uname -a"}, 0)
	ts.Send(tuplespace.Tuple{"cmd": "uptime"}, 0)
	_, err := ts.Take(tuplespace.MustMatch(`cmd != nil`), time.Second)
	assert.NoError(t, err)
	assert.Equal(t, ts.Stats().Tuples, 1)
}
Esempio n. 20
0
func TestBinaryDecodeToValueErrors(t *testing.T) {
	b := []byte{1, 0, 0, 0}
	var v uint32
	err := Unmarshal(b, v)
	assert.Error(t, err)
	err = Unmarshal(b, &v)
	assert.NoError(t, err)
	assert.Equal(t, uint32(1), v)
}
Esempio n. 21
0
func TestNoBool(t *testing.T) {
	fg := newFlagGroup()
	f := fg.Flag("b", "").Default("true")
	b := f.Bool()
	fg.init()
	tokens := Tokenize([]string{"--no-b"})
	_, err := fg.parse(tokens, false)
	assert.NoError(t, err)
	assert.False(t, *b)
}
Esempio n. 22
0
func TestRequiredFlags(t *testing.T) {
	c := New("test", "test")
	c.Flag("a", "a").String()
	c.Flag("b", "b").Required().String()

	_, err := c.Parse([]string{"--a=foo"})
	assert.Error(t, err)
	_, err = c.Parse([]string{"--b=foo"})
	assert.NoError(t, err)
}
Esempio n. 23
0
func BenchmarkClientServer(b *testing.B) {
	sm, cm := newServerAndClient()
	wg := &sync.WaitGroup{}

	wg.Add(1)

	// Server
	go func() {
		buf := make([]byte, 1024)
		defer wg.Done()
		ch, err := sm.Accept()
		assert.NoError(b, err)
		if err != nil {
			return
		}
		defer ch.Close()
		for i := 0; i < b.N; i++ {
			_, err := ch.Write(buf)
			assert.NoError(b, err)
			if err != nil {
				return
			}
		}
	}()

	ch, err := cm.Dial()
	assert.NoError(b, err)

	buf := make([]byte, 1024)
	for i := 0; i < b.N; i++ {
		n, err := ch.Read(buf)
		assert.NoError(b, err)
		if err != nil {
			b.FailNow()
		}
		assert.Equal(b, 1024, n)
	}

	sm.Close()
	cm.Close()
	wg.Wait()
}
Esempio n. 24
0
func TestTupleSpaceReadAll(t *testing.T) {
	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())
	defer ts.Shutdown()
	ts.Send(tuplespace.Tuple{"cmd": "uname -a"}, 0)
	ts.Send(tuplespace.Tuple{"cmd": "uptime"}, 0)
	time.Sleep(time.Millisecond * 100)
	tuples, err := ts.ReadAll(tuplespace.MustMatch(`cmd != nil`), time.Second)
	assert.NoError(t, err)
	assert.Equal(t, tuples, []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}, tuplespace.Tuple{"cmd": "uptime"}})
	assert.Equal(t, ts.Stats().Tuples, 2)
}
Esempio n. 25
0
func TestDispatchCallbackIsCalled(t *testing.T) {
	dispatched := false
	c := New("test", "")
	c.Command("cmd", "").Dispatch(func(*ParseContext) error {
		dispatched = true
		return nil
	})

	_, err := c.Parse([]string{"cmd"})
	assert.NoError(t, err)
	assert.True(t, dispatched)
}
Esempio n. 26
0
func TestNestedCommandsWithArgs(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Arg("a", "").String()
	b := cmd.Arg("b", "").String()
	context := Tokenize([]string{"a", "b", "c", "d"})
	selected, err := app.parse(context)
	assert.NoError(t, err)
	assert.Equal(t, 0, len(context.Tokens))
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "c", *a)
	assert.Equal(t, "d", *b)
}
Esempio n. 27
0
func TestArgMultipleRequired(t *testing.T) {
	a := newArgGroup()
	a.Arg("a", "").Required().String()
	a.Arg("b", "").Required().String()
	a.init()

	_, err := a.parse(Tokenize([]string{}))
	assert.Error(t, err)
	_, err = a.parse(Tokenize([]string{"A"}))
	assert.Error(t, err)
	_, err = a.parse(Tokenize([]string{"A", "B"}))
	assert.NoError(t, err)
}
Esempio n. 28
0
func TestMapStatic(t *testing.T) {

	codecService := new(codecservices.WebCodecService)
	h := NewHttpHandler(codecService)

	h.MapStatic("/static", "/location/of/static")

	assert.Equal(t, 1, len(h.HandlersPipe()))

	staticHandler := h.HandlersPipe()[0].(*PathMatchHandler)

	if assert.Equal(t, 1, len(staticHandler.HttpMethods)) {
		assert.Equal(t, goweb_http.MethodGet, staticHandler.HttpMethods[0])
	}

	var ctx context.Context
	var willHandle bool

	ctx = context_test.MakeTestContextWithPath("/static/some/deep/file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/../static/some/deep/file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/some/../file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/../file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.False(t, willHandle, "Static handler should not handle")

	ctx = context_test.MakeTestContextWithPath("/static")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/doc.go")
	willHandle, _ = staticHandler.WillHandle(ctx)
	_, staticHandleErr := staticHandler.Handle(ctx)

	if assert.NoError(t, staticHandleErr) {

	}

}
Esempio n. 29
0
// https://github.com/stretchrcom/goweb/issues/20
func TestWriteResponseObject_ContentNegotiation_AcceptHeader(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := new(codecservices.WebCodecService)
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	ctx.HttpRequest().Header.Set("Accept", "application/x-msgpack")
	data := map[string]interface{}{"name": "Mat"}

	API.WriteResponseObject(ctx, 200, data)

	// get the expected output
	codec, codecErr := codecService.GetCodec("application/x-msgpack")
	if assert.NoError(t, codecErr) {

		expectedOutput, marshalErr := codec.Marshal(data, nil)
		if assert.NoError(t, marshalErr) {
			assert.Equal(t, []byte(context_test.TestResponseWriter.Output), expectedOutput)
		}

	}

}
Esempio n. 30
0
func TestTupleSpaceProcessNewEntriesAlwaysRespondsToReaders(t *testing.T) {
	rts := tuplespace.NewRawTupleSpace(store.NewMemoryStore())
	take := rts.ReadOperation(tuplespace.MustMatch("cmd"), 0, tuplespace.ActionOne|tuplespace.ActionTake)
	read := rts.ReadOperation(tuplespace.MustMatch("cmd"), 0, tuplespace.ActionOne)
	go func() {
		time.Sleep(time.Second * 1)
		take.Cancel()
		read.Cancel()
	}()
	tuples := []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}}
	expected := []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}}
	assert.NoError(t, rts.ProcessNewEntries(tuples, time.Now()))
	assert.Equal(t, <-take.Get(), expected)
	assert.Equal(t, <-read.Get(), expected)
}