Ejemplo n.º 1
0
func TestFoo(t *testing.T) {
	err := instances.New("1", "5", reflect.ValueOf(&FooFactory{}), owners)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	ff := &FooFactoryClient{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "5",
		PointerRecv: true,
	}

	err = ff.Init(true)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	defer ff.CloseClient()

	foo := ff.Make("test")
	if err = ff.Error(); err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	if foo.String() != "foo:test" {
		t.Fatal("foo returned the wrong string")
	}
}
Ejemplo n.º 2
0
func BenchmarkBoltDbBuffer(b *testing.B) {
	name, err := rand.FileName("boltdb", ".db", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + "/" + name
	gob := &Gob{
		TypeName: types.Name(&log{}),
	}
	bolt, err := NewBoltDb("test", name, 0600, nil, gob, gob)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	logger := New(
		NewOutBuffer(
			NewGeneric(bolt).F(DefFormatter),
			b.N/2,
		),
		false,
	).Domain("test")
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Print(msg)
		b.SetBytes(l)
	}
}
Ejemplo n.º 3
0
func TestStruct1(t *testing.T) {
	err := instances.New("1", "2", reflect.ValueOf(&Struct1{}), owners)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	s1 := &Struct1Client{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "2",
		PointerRecv: true,
	}

	str, err := s1.Func1()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if str == nil {
		t.Fatal("nil str")
	}
	if *str != "string" {
		t.Fatal("wrong string", *str)
	}

	err = s1.CloseClient()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 4
0
func TestEditPlainTextCredentials(t *testing.T) {
	ptc := NewPlainTextCredentials()

	pt1 := &PlainText{"id", "pass1234"}

	err := ptc.Add(pt1)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	pt1b := &PlainText{"id", "pass2345"}

	err = ptc.Edit(pt1b)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	auth, err := ptc.Get("id")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if !auth.Auth(pt1b) {
		t.Fatal("auth not equal")
	}

	pt2 := &PlainText{"id2", "pass2345"}
	err = ptc.Edit(pt2)
	if err != nil && !e.Equal(err, ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 5
0
func TestGet(t *testing.T) {
	inst, err := instances.Get("1", "1")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if inst.Instance().Type() != reflect.TypeOf(&teststruct{}) {
		t.Fatal("get fail")
	}

	inst, err = instances.Get("1", "3")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if inst.Instance().Type() != reflect.TypeOf(&teststruct{}) {
		t.Fatal("get fail")
	}

	_, err = instances.Get("1", "1")
	if err != nil && !e.Equal(err, ErrAuthUserNotAuthorized) {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	_, err = instances.Get("2", "2")
	if err != nil && !e.Equal(err, ErrSessionNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = instances.Get("1", "2")
	if err != nil && !e.Equal(err, ErrInstanceNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 6
0
func TestInitClones(t *testing.T) {
	connPool = &clones{
		Num:    1,
		MaxNum: 5,
	}
	err := connPool.Init()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	connPoolErr := &clones{
		Num:    0,
		MaxNum: 5,
	}
	err = connPoolErr.Init()
	if err != nil && !e.Equal(err, "number of clones invalid") {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("nil error")
	}

	connPoolErr = &clones{
		Num:    10,
		MaxNum: 5,
	}
	err = connPoolErr.Init()
	if err != nil && !e.Equal(err, "number of streams is greater than max") {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("nil error")
	}
}
Ejemplo n.º 7
0
func TestAuthSession(t *testing.T) {
	a := &auth.PlainText{"id3", "pass6969"}
	err := instances.AuthSession("sess", a)
	if err != nil && !e.Equal(err, ErrSessionNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = instances.AuthSession("1", a)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = instances.AuthSession("1", &auth.PlainText{"id3", "pass69222269"})
	if err != nil && !e.Equal(err, ErrAuthUserNotAuthorized) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}
	err = instances.AuthSession("1", &auth.PlainText{"id4", "pass69222269"})
	if err != nil && !e.Equal(err, ErrAuthUserNotAuthorized) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}
}
Ejemplo n.º 8
0
func TestBigPriority(t *testing.T) {
	var numQueues uint64 = 1<<64 - 1
	q := NewPriority(numQueues, func() (Queue, error) {
		q := NewSimple(10, 1)
		return q, nil
	})
	defer q.Stop()
	q.Process()
	job := job.NewSimple("name", numQueues-1, func() error {
		delta, err := rand.Int63n(1000, "go")
		if err != nil {
			return e.Forward(err)
		}
		delta++
		time.Sleep(time.Duration(delta) * time.Millisecond)
		return nil
	})
	err := q.Add(job)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	job.Returns(&err)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 9
0
func TestBarrierWriterReaderPartial(t *testing.T) {
	buf := NewBuffer()
	defer buf.Close()
	b, err := rand.Bytes(1e6, "go")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	n, err := buf.Write(b)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if n != len(b) {
		t.Fatal("length invalid")
	}
	b2 := make([]byte, len(b))
	count := 0
	l := 100
	for count < len(b) {
		n, err = buf.Read(b2[count : count+l])
		if err == io.EOF {
			break
		} else if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		count += n
	}
	if count != len(b) {
		t.Fatal("length invalid", count, len(b))
	}
	if !bytes.Equal(b, b2) {
		t.Fatal("not equal")
	}
}
Ejemplo n.º 10
0
func TestAddPlainTextCredentials(t *testing.T) {
	ptc := NewPlainTextCredentials()

	pt1 := &PlainText{"id", "pass1234"}
	pt1b := &PlainText{"id", "pass1234"}
	pt2 := &PlainText{"id2", "pass2345"}
	hash := &Hashed{"a", "b"}

	err := ptc.Add(pt1)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Add(pt2)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Add(pt1b)
	if err != nil && !e.Equal(err, ErrCredExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Add(hash)
	if err != nil && !e.Equal(err, ErrNotComp) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 11
0
func TestValidate(t *testing.T) {
	ptc := NewPlainTextCredentials()

	pt1 := &PlainText{"id", "passpass"}
	pt2 := &PlainText{"id2", "passpass2"}
	pt3 := &PlainText{"id3", "passpass2"}
	pt1b := &PlainText{"id", "passpass2"}

	err := ptc.Add(pt1)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Add(pt2)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Add(pt3)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	if err := Validate(ptc, pt1); err != nil {
		t.Fatal("invalid")
	}
	if err := Validate(ptc, pt2); err != nil {
		t.Fatal("invalid")
	}
	if err := Validate(ptc, pt3); err != nil {
		t.Fatal("invalid")
	}
	if err := Validate(ptc, pt1b); err == nil {
		t.Fatal("invalid")
	}
}
Ejemplo n.º 12
0
func TestProtoReqInst(t *testing.T) {
	buf := bytes.NewBuffer([]byte{})
	enc := msgpack.NewEncoder(buf)
	dec := msgpack.NewDecoder(buf)

	req := &ReqInst{
		Inst: "inst",
		Obj:  "obj",
		Args: []interface{}{"string", int(42)},
	}

	err := enc.Encode(req)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	recv := ReqInst{}

	err = dec.Decode(&recv)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	if recv.Inst != "inst" || recv.Obj != "obj" || len(recv.Args) != 2 || recv.Args[0].(string) != "string" || recv.Args[1].(int) != 42 {
		t.Fatal("don't decode")
	}
}
Ejemplo n.º 13
0
func BenchmarkMongoDbBuffer(b *testing.B) {
	mongodb, err := NewMongoDb("mongodb://localhost/test", "test", nil, Log, 30*time.Second)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	err = mongodb.Drop()
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	logger := New(
		NewOutBuffer(
			NewGeneric(mongodb).F(DefFormatter),
			b.N/2,
		),
		false,
	).Domain("test")
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Print(msg)
		b.SetBytes(l)
	}
	b.StopTimer()
	err = mongodb.Drop()
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 14
0
func put(t *testing.T, s Storer, num int) {
	err := s.Tx(true, func(tx Transaction) error {
		var err error
		for i := 0; i < num; i++ {
			key := strconv.Itoa(i)
			err = tx.Put(key, &TestStruct{I: i, Key: key})
			if err != nil {
				return err
			}
		}
		err = tx.Put("3a", &TestStruct{I: 3, Key: "3a"})
		if err != nil {
			return err
		}
		err = tx.Put("3a", &TestStruct{I: 3, Key: "3a"})
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	l, err := s.Len()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if l != uint(num+1) {
		t.Fatalf("wrong len %v %v", l, num)
	}
}
Ejemplo n.º 15
0
func del(t *testing.T, s Storer, num int) {
	err := s.Tx(true, func(tx Transaction) error {
		var err error
		for i := 0; i < num; i++ {
			key := strconv.Itoa(i)
			err = tx.Del(key)
			if err != nil {
				return err
			}
		}
		err = tx.Del("3a")
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	l, err := s.Len()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if l != 0 {
		t.Fatalf("something remining %v", l)
	}
}
Ejemplo n.º 16
0
func BenchmarkLogOuterFile(b *testing.B) {
	name, err := rand.FileName("BenchmarkLogFile", ".log", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + "/" + name
	file, err := os.Create(name)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	defer os.Remove(name)
	defer file.Close()

	backend := NewWriter(file).F(DefFormatter)
	olog, ok := backend.(OuterLogger)
	if !ok {
		return
	}
	w := olog.OuterLog(DebugPrio, "tag")
	defer olog.Close()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		n, err := w.Write([]byte(msg + "\n"))
		if err != nil {
			b.Error(e.Trace(e.Forward(err)))
		}
		if int64(n) != l+1 {
			b.Error("write failed", n, l)
		}
		b.SetBytes(l)
	}
}
Ejemplo n.º 17
0
func TestDeletePlainTextCredentials(t *testing.T) {
	ptc := NewPlainTextCredentials()

	pt1 := &PlainText{"id", "pass1234"}

	err := ptc.Add(pt1)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = ptc.Get("id")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Delete("id")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = ptc.Delete("id")
	if err != nil && !e.Equal(err, ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = ptc.Get("id")
	if err != nil && !e.Equal(err, ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 18
0
func TestExp(t *testing.T) {
	client := &TestExportClient{
		Net:      "tcp",
		Addrs:    serverTest.Address().String(),
		Auth:     &auth.PlainText{"id", "pass1234"},
		Session:  "1",
		Instance: "2",
		Interval: time.Second,
	}
	err := client.Init(true)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	ret := client.TestRetval()
	if ret.Calc() != 42 {
		t.Fatal("wrong anwser")
	}
	// err = client.Destroy()
	// if err != nil {
	// 	t.Fatal(e.Trace(e.Forward(err)))
	// }
	err = client.CloseClient()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 19
0
func BenchmarkLogFileBuffer(b *testing.B) {
	name, err := rand.FileName("BenchmarkLogFile", ".log", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + name
	file, err := os.Create(name)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	defer os.Remove(name)
	defer file.Close()

	back := NewOutBuffer(
		NewWriter(file).F(DefFormatter),
		b.N/2,
	)
	defer back.(*OutBuffer).Close()

	logger := New(back, false).Domain("test")

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		logger.Print(msg)
		b.SetBytes(l)
	}
}
Ejemplo n.º 20
0
func TestConstructFail(t *testing.T) {
	err := constructor.New(&struct3{}, func(count int) int {
		return 42
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = constructor.Construct(types.Name(&struct3{}), 0)
	if err != nil && !e.Equal(err, ErrInvReturn) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = constructor.Delete(types.Name(&struct3{}))
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = constructor.New(&struct3{}, func(count int) (int, int) {
		return 42, 69
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = constructor.Construct(types.Name(&struct3{}), 0)
	if err != nil && !e.Equal(err, ErrInvReturn) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}
}
Ejemplo n.º 21
0
Archivo: smtp.go Proyecto: fcavani/net
// ExecTimeout executes f with arguments args and return a function of type Return.
// Return must be called with the pointer to the return values.
func (c *Command) ExecTimeout(timeout time.Duration, f interface{}, args ...interface{}) Return {
	var retvals chan []reflect.Value = make(chan []reflect.Value)
	val := reflect.ValueOf(f)
	if val.Kind() != reflect.Func {
		panic("f is not a function")
	}
	t := val.Type()
	if t.NumIn() != len(args) {
		panic("invalid number of arguments")
	}
	a := make([]reflect.Value, len(args))
	for i, arg := range args {
		a[i] = reflect.ValueOf(arg)
		if !a[i].Type().AssignableTo(t.In(i)) {
			panic(fmt.Sprintf("invalid argument type, argument %v must be assignable to %v", a[i].Type(), t.In(i)))
		}
	}
	go func() {
		var err error
		if timeout != 0 {
			err = c.Conn.SetDeadline(time.Now().Add(timeout))
			if err != nil {
				log.Println("SetDeadline failed with error:", e.Trace(e.Forward(err)))
			}
		} else {
			err = c.Conn.SetDeadline(time.Time{})
			if err != nil {
				log.Println("SetDeadline failed with error:", e.Trace(e.Forward(err)))
			}
		}
		values := val.Call(a)
		if timeout != 0 {
			err = c.Conn.SetDeadline(time.Time{})
			if err != nil {
				log.Println("SetDeadline failed with error:", e.Trace(e.Forward(err)))
			}
		}
		setError(values, err)
		retvals <- values
	}()

	return func(args ...interface{}) {
		retvals := <-retvals
		if len(retvals) != len(args) {
			panic(fmt.Sprintf("the number of arguments (%v) in Returns must be equal to the number of return values in the function (%v)", len(args), len(retvals)))
		}
		for i, retval := range retvals {
			val := reflect.ValueOf(args[i])
			if val.Kind() != reflect.Ptr {
				panic("Returns arguments must be pointers")
			}
			if retval.Type() != val.Elem().Type() {
				panic("diferent kind")
			}
			val.Elem().Set(retval)
		}
	}
}
Ejemplo n.º 22
0
func TestStream(t *testing.T) {
	//ulimit -n 1000
	//defer leaktest.Check(t)()

	var wg sync.WaitGroup

	t.Log("Num:", numClientTest)

	wg.Add(numClientTest)

	s1 := &Struct1Client{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "2",
		PointerRecv: true,
	}

	err := s1.Init(true)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	var lck sync.Mutex

	for i := 0; i < numClientTest; i++ {
		go func(i int) {
			defer wg.Done()
			n, err := rand.Int63n(100, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("Waiting...", i)
			time.Sleep(time.Duration(n) * time.Millisecond)
			//println("Running...", i)
			n, err = rand.Int63n(500, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			s1.Wait(time.Duration(n) * time.Millisecond)
			lck.Lock()
			err = s1.Error()
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			lck.Unlock()
			//println("End...", i)
		}(i)
	}
	wg.Wait()
	//println("Close...")
	err = s1.CloseClient()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 23
0
func TestFullWriteRead(t *testing.T) {
	const num = 1000
	const maxmax = 1e6
	const size = 1e6
	bufs := make([][]byte, num)
	numGoStart := runtime.NumGoroutine()
	buf := NewBuffer()
	defer func() {
		buf.Close()
		numGoStop := runtime.NumGoroutine()
		t.Log("NumGoroutine:", numGoStart, numGoStop, numGoStop-numGoStart)
	}()
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(size, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		in, err := rand.Bytes(int(n)+1, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = in
		_, err = buf.Write(in)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(maxmax, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		max := int(n) + 1

		in := bufs[i]
		size := len(in)
		if size > max {
			size = max
		}
		b := make([]byte, len(in))

		for count := 0; count < len(in); {
			end := len(b)
			if count+size < end {
				end = count + size
			}
			n, err := buf.Read(b[count:end])
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			count += n
		}
		if !bytes.Equal(in, b) {
			t.Fatal("data not equal")
		}
	}
}
Ejemplo n.º 24
0
func TestDeleteConstructor(t *testing.T) {
	err := constructor.Delete(types.Name(&struct1{}))
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = constructor.Delete(types.Name(&struct1{}))
	if err != nil && !e.Equal(err, ErrConstNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 25
0
func TestLdap(t *testing.T) {
	url := testParse(t, ldapUrl)
	err := PingLdap(url)
	if err != nil {
		t.Log(e.Trace(e.Forward(err)))
	}
	err = PingLdapTLS(url)
	if err != nil {
		t.Log(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 26
0
func TestProtoWriteReadPartial(t *testing.T) {
	const num = 10
	const maxmax = 1e2
	const size = 1e6
	bufs := make([][]byte, num)
	fifo := NewNotAFifo(100)
	defer fifo.Close()
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(size, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		in, err := rand.Bytes(int(n)+1, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = in
		err = fifo.Write(in)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(maxmax, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		max := int(n) + 1

		in := bufs[i]
		size := len(in)
		if size > max {
			size = max
		}
		buf := make([]byte, len(in))

		for count := 0; count < len(in); {
			end := len(buf)
			if count+size < end {
				end = count + size
			}
			n, err := fifo.Read(buf[count:end])
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}

			count += n
		}
		if !bytes.Equal(in, buf) {
			t.Fatal("data not equal")
		}
	}
}
Ejemplo n.º 27
0
func TestServerIpv4mc(t *testing.T) {
	in, err := Discover(net.FlagMulticast)
	if e.Equal(err, ErrNoInt) {
		t.Log("No multicast capable interface, may be this is travis.cl. Skip the test.")
		return
	} else if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	server := &Server{}
	server.Name = "master"
	server.PrivateKey = MasterKey
	server.PubKeys = Keys
	server.Interface = in
	server.AddrVer = Ipv4
	server.Protocol = func(addr *net.UDPAddr, req *Request) (resp *Response, err error) {
		if string(req.Data) != "request" {
			return nil, e.New("protocol error")
		}
		t.Log(req)
		return &Response{
			Data: []byte("msg"),
		}, nil
	}
	err = server.Do()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	defer server.Close()

	client := &Client{}
	client.ServerName = "master"
	client.ServerKey = &MasterKey.PublicKey
	client.Name = "slave"
	client.PrivateKey = SlaveKey
	client.Interface = in
	client.AddrVer = Ipv4
	client.Port = server.Port
	client.Request = func(dst *net.UDPAddr) (*Request, error) {
		return &Request{
			Data: []byte("request"),
		}, nil
	}
	resp, err := client.Discover()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	if string(resp.Data) != "msg" {
		t.Fatal("received wrong message", string(resp.Data))
	}
	t.Log(resp)
}
Ejemplo n.º 28
0
func BenchmarkLoad(b *testing.B) {
	instances := core.NewInstances()

	owners := auth.NewPlainTextCredentials()
	id = &auth.PlainText{Ident: "id", Pass: "******"}
	owners.Add(id)

	constructor := core.NewConstructor()

	server = &core.Server{
		Proto:       "tcp",
		Addr:        "localhost:0",
		ConnTimeout: 30 * time.Second,
		Insts:       instances,
		Cons:        constructor,
		FifoLength:  1,
		Ttl:         24 * time.Hour,
		Cleanup:     5 * time.Minute,
	}
	server.Start()

	err := instances.New("1", "3", reflect.ValueOf(&Load{}), owners)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}

	l := &LoadClient{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "3",
		PointerRecv: true,
	}

	err = l.Init(true)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}

	in, err := rand.Bytes(1e6, "go")
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		l.Load(in)
		b.SetBytes(int64(len(in)))
	}
}
Ejemplo n.º 29
0
func TestClonesReturnFail(t *testing.T) {
	conn, err := connPool.Request(p, "0")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = connPool.Return("1", "0", conn)
	if err != nil && !e.Equal(err, ErrSessNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = connPool.Return("0", "1", conn)
	if err != nil && !e.Equal(err, ErrInstNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
Ejemplo n.º 30
0
func TestInit(t *testing.T) {
	var err error
	p, err = openSession("tcp", server.Address().String(), nil, "0", id, time.Second, 0)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	c = &clone{
		Num:     1,
		MaxNum:  5,
		Session: p,
	}
	err = c.Init("0")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	cerr := &clone{
		Num:     5,
		MaxNum:  1,
		Session: p,
	}
	err = cerr.Init("0")
	if err != nil && !e.Equal(err, "number of initial streams is greater than the maximun") {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("nil error")
	}
	cerr = &clone{
		Num:     0,
		MaxNum:  1,
		Session: p,
	}
	err = cerr.Init("0")
	if err != nil && !e.Equal(err, "number of clones invalid") {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("nil error")
	}
	cerr = &clone{
		Num:     1,
		MaxNum:  3,
		Session: nil,
	}
	err = cerr.Init("0")
	if err != nil && !e.Equal(err, "nil Session") {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("nil error")
	}
}