Пример #1
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)))
	}
}
Пример #2
0
func BenchmarkCall(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", "2", reflect.ValueOf(&Struct1{}), owners)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}

	s1 := &Struct1Client{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        &auth.PlainText{"id", "pass1234"},
		Session:     "1",
		Instance:    "2",
		PointerRecv: true,
	}
	err = s1.Init(true)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		s1.Func1()
	}
}
Пример #3
0
func TestServer(t *testing.T) {
	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()
}
Пример #4
0
func Example() {
	// Create a database of credentials
	owners := auth.NewPlainTextCredentials()
	// Authorize the user id
	err := owners.Add(&auth.PlainText{"id", "pass1234"})
	if err != nil {
		log.Error(err)
	}
	// Constructors
	constructor := core.NewConstructor()
	// Struct with the instances
	instances := core.NewInstances()
	// New instance of a calculator.
	err = instances.New("1", "1", reflect.ValueOf(dummy.Calculator{}), owners)
	if err != nil {
		log.Error(err)
	}
	// Export the return value 0 of BigAdd with the client ResultClient
	err = instances.Export("1", "1", &core.ToExport{Method: "BigAdd", RetvalPos: 0, Export: &core.Export{Client: &dummy.ResultClient{}}})
	if err != nil {
		log.Error(err)
	}

	// Create the server
	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,
	}

	// Start the server
	server.Start()
	defer server.Close()

	// Configure the client
	calc := &dummy.CalculatorClient{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        &auth.PlainText{"id", "pass1234"},
		Session:     "1",
		Instance:    "1",
		PointerRecv: true,
	}

	// Use the client API
	result := calc.Add(2, 40)
	err = calc.Error()
	if err != nil {
		log.Error(err)
	}
	fmt.Println(result)

	res := calc.BigAdd(2, 40)
	err = calc.Error()
	if err != nil {
		log.Error(err)
		return
	}
	fmt.Println(res.Get())

	err = calc.Destroy()
	if err != nil {
		log.Error(err)
	}
	//Output:
	//42
	//42
}
Пример #5
0
func main() {
	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", "2", reflect.ValueOf(&dummy.Struct1{}), owners)
	if err != nil {
		log.Fatal(e.Trace(e.Forward(err)))
	}
	s1 := &dummy.Struct1Client{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "2",
		PointerRecv: true,
	}

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

	log.Println("Destroy...")
	err = s1.Destroy()
	if err != nil {
		log.Fatal(e.Trace(e.Forward(err)))
	}
	log.Println("Close client...")
	err = s1.CloseClient()
	if err != nil {
		log.Fatal(e.Trace(e.Forward(err)))
	}
	log.Println("Close client...")
	err = server.Close()
	if err != nil {
		log.Fatal(e.Trace(e.Forward(err)))
	}

	panic("hi")
}