Пример #1
0
func TestServicBasics(t *testing.T) {
	a := new(appdescriptor.AppDescriptor)
	s1 := NewService(a, EXPORTING, config.DefaultLocalhost(), []byte{0})
	defer s1.Remove()
	c := s1.Disconnected().AsChan()
	c1 := s1.Connected().AsChan()
	s1.Run()
	s2 := NewService(a, IMPORTING, config.DefaultLocalhost(), []byte{0})
	c2 := s2.Connected().AsChan()
	s2.Run()

	select {
	case <-time.After(15 * time.Second):
		t.Error("Service 1 Did Not Connect")

	case <-c1:
	}
	select {
	case <-time.After(1 * time.Second):
		t.Error("Service 2 Did Not Connect")

	case <-c2:
	}

	t.Log("Shutting Down Service 2")

	s2.Remove()
	select {
	case <-time.After(10 * time.Second):
		t.Error("Service 2 Did Not Disconnect")

	case <-c:
		t.Log("Successfully Disconnected Service 2")
	}
}
Пример #2
0
func TestNodeDiscover(t *testing.T) {
	n1 := node.New("test1", config.DefaultLocalhost(), nil)
	defer n1.Shutdown()
	j1 := n1.Join().AsChan()
	n1.Run()

	n2 := node.New("test2", config.DefaultLocalhost(), nil)
	defer n2.Shutdown()
	j2 := n2.Join().AsChan()
	n2.Run()
	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Couldnt find network.node 1")
	case data := <-j1:
		if !strings.Contains(data.(serf.Member).Name, n2.UUID) {
			t.Error("Found wrong UUID")
		}
	}

	select {
	case <-time.After(5 * time.Minute):
		t.Fatal("Couldnt find network.node 2")
	case data := <-j2:
		if !strings.Contains(data.(serf.Member).Name, n1.UUID) {
			t.Error("Found wrong UUID")
		}
	}
}
func TestExportReactor(t *testing.T) {
	i := aursir4go.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i.Remove()
	e := patterns.NewExportReactor(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e.Stop()
	i.Run()
	params := []byte{4, 5, 63, 4}
	e.React("SayHello", func(r *messages.Request) interface{} {
		return params
	})
	e.Start()
	i.Connected().WaitUntilComplete()
	f := i.Call("SayHello", params)

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Request")
	case d := <-f.AsChan():
		r := d.(*messages.Result)

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
	}
}
Пример #4
0
func NewAnnouncer(uuid string, addresses []string, servicetype string, desc *appdescriptor.AppDescriptor) (a *Announcer) {

	cfg := config.DefaultLocalhost()

	a = new(Announcer)
	a.announced = eventual2go.NewCompleter()
	a.logger = log.New(cfg.Logger(), fmt.Sprintf("announcer %s ", uuid), log.Lshortfile)

	a.new = eventual2go.NewStreamController()
	a.servicetype = servicetype
	addrs := []string{}
	a.clientPorts = map[string]int{}

	for _, addr := range addresses {
		as := strings.Split(addr, ":")
		addrs = append(addrs, as[0])
		p, _ := strconv.ParseInt(as[1], 0, 0)
		a.clientPorts[as[0]] = int(p)
		a.logger.Println("adding address", as[0], int(p))
	}

	cfg.NetworkInterfaces = addrs
	a.node = node.New(uuid, cfg, desc.AsTagSet())

	a.r = eventual2go.NewReactor()
	a.r.React("first_join", a.announce)
	a.r.AddFuture("first_join", a.node.Join().First())
	a.r.React("service_found", a.replyToServiceQuery)
	a.r.AddStream("service_found", a.node.Queries().WhereNot(isService(a.servicetype)))
	a.logger.Println("setup finished")
	return
}
Пример #5
0
func TestCallOne2One(t *testing.T) {
	i := aursir4go.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i.Remove()
	e := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e.Remove()
	c := e.Requests().AsChan()
	i.Run()

	e.Run()
	i.Connected().WaitUntilComplete()
	e.Connected().WaitUntilComplete()
	params := []byte{4, 5, 63, 4}
	f := i.Call("SayHello", params)

	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Didnt Got Request")
	case d := <-c:
		r := d.(*messages.Request)
		if r.Importer != i.UUID() {
			t.Error("Wrong Import UUID", r.Importer, i.UUID())
		}
		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
		e.Reply(r, params)
	}

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Request")
	case d := <-f.AsChan():
		r := d.(*messages.Result)
		if r.Exporter != e.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
	}
}
Пример #6
0
func TestEmit(t *testing.T) {
	i := aursir4go.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	e := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e.Remove()
	c := i.Results().AsChan()
	i.Listen("SayHello")
	i.Run()

	e.Run()
	i.Connected().WaitUntilComplete()
	e.Connected().WaitUntilComplete()

	params := []byte{4, 5, 63, 4}
	e.Emit("SayHello", nil, params)

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result")
	case d := <-c:
		r := d.(*messages.Result)
		if r.Exporter != e.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
	}
	i.StopListen("SayHello")
	time.Sleep(100 * time.Millisecond)
	e.Emit("SayHello", nil, params)

	select {
	case <-time.After(2 * time.Microsecond):
	case <-c:
		t.Fatal("Got Result")
	}
	i.Remove()
	time.Sleep(1 * time.Second)
	//testing if it not crashes due to uncomplete removal
	e.Emit("SayHello", nil, params)
}
Пример #7
0
func TestNodeLeave(t *testing.T) {
	n1 := node.New("test1", config.DefaultLocalhost(), nil)
	defer n1.Shutdown()
	c := n1.Leave().AsChan()
	n1.Run()

	n2 := node.New("test2", config.DefaultLocalhost(), nil)
	n2.Run()

	time.Sleep(3 * time.Second)

	n2.Leave()
	n2.Shutdown()

	select {
	case <-time.After(30 * time.Second):
		t.Fatal("network.node didnt leave")
	case data := <-c:
		if !strings.Contains(data.(node.LeaveEvent).Name, n2.UUID) {
			t.Error("Found wrong UUID")
		}
	}
}
Пример #8
0
func TestCallOne2Many(t *testing.T) {
	i := aursir4go.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i.Remove()
	i.Run()
	e1 := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e1.Remove()
	c1 := e1.Requests().AsChan()
	e1.Run()
	e2 := aursir4go.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e2.Remove()
	c2 := e2.Requests().AsChan()
	e2.Run()

	i.Connected().WaitUntilComplete()
	e1.Connected().WaitUntilComplete()
	e2.Connected().WaitUntilComplete()

	params := []byte{4, 5, 63, 4}
	params1 := []byte{3}
	params2 := []byte{6}
	s := eventual2go.NewStreamController()
	s1, s2 := s.Split(func(d eventual2go.Data) bool { return d.(*messages.Result).Exporter == e1.UUID() })
	rc1 := s1.AsChan()
	rc2 := s2.AsChan()
	i.CallAll("SayHello", params, s)
	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Didnt Got Request 1")
	case d := <-c1:
		r := d.(*messages.Request)
		if r.Importer != i.UUID() {
			t.Error("Wrong Import UUID 1", r.Importer, i.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params 1", r.Parameter(), params)
		}
		e1.Reply(r, params1)
	}

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Request 2")
	case d := <-c2:
		r := d.(*messages.Request)
		if r.Importer != i.UUID() {
			t.Error("Wrong Import UUID 2", r.Importer, i.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params) {
			t.Error("Wrong Params 2", r.Parameter(), params)
		}
		e2.Reply(r, params2)
	}

	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result 1")
	case d := <-rc1:
		r := d.(*messages.Result)
		if r.Exporter != e1.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e1.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params1) {
			t.Error("Wrong Params", r.Parameter(), params1)
		}
	}
	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result 2")
	case d := <-rc2:
		r := d.(*messages.Result)
		if r.Exporter != e2.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e2.UUID())
		}

		var res []byte
		r.Decode(&res)
		if !bytes.Equal(res, params2) {
			t.Error("Wrong Params", r.Parameter(), params2)
		}
	}

}
Пример #9
0
func TestMultipleServices(t *testing.T) {
	a := new(appdescriptor.AppDescriptor)
	s1 := NewService(a, EXPORTING, config.DefaultLocalhost(), []byte{0})
	defer s1.Remove()
	c1 := s1.Connected().AsChan()
	s1.Run()

	s2 := NewService(a, IMPORTING, config.DefaultLocalhost(), []byte{0})
	defer s2.Remove()
	c2 := s2.Connected().AsChan()
	s2.Run()

	s3 := NewService(a, IMPORTING, config.DefaultLocalhost(), []byte{0})
	defer s3.Remove()
	c3 := s3.Connected().AsChan()
	s3.Run()

	s4 := NewService(a, EXPORTING, config.DefaultLocalhost(), []byte{0})
	defer s4.Remove()
	c4 := s4.Connected().AsChan()
	s4.Run()

	select {
	case <-time.After(15 * time.Second):
		t.Error("Service 1 Did Not Connect")

	case <-c1:
		if len(s1.connectedServices) != 2 {
			t.Error("Service 1 didnt connet to all")
		}
	}
	select {
	case <-time.After(1 * time.Second):
		t.Error("Service 2 Did Not Connect")

	case <-c2:
		if len(s2.connectedServices) != 2 {
			t.Error("Service 2 didnt connet to all")
		}
	}

	select {
	case <-time.After(1 * time.Second):
		t.Error("Service 3 Did Not Connect")

	case <-c3:
		if len(s3.connectedServices) != 2 {
			t.Error("Service 3 didnt connet to all")
		}
	}

	select {
	case <-time.After(1 * time.Second):
		t.Error("Service 4 Did Not Connect")

	case <-c4:
		if len(s3.connectedServices) != 2 {
			t.Error("Service 4 didnt connet to all")
		}
	}

}
Пример #10
0
func TestCallMany2One(t *testing.T) {
	i1 := aurarath.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i1.Remove()
	i2 := aurarath.NewImport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer i2.Remove()
	e := aurarath.NewExport(TEST_APP_DESCRIPTOR, config.DefaultLocalhost())
	defer e.Remove()
	c := e.Requests().AsChan()

	c1 := i1.Results().AsChan()
	c2 := i2.Results().AsChan()

	e.Run()
	i2.Run()
	i1.Run()

	i1.Connected().WaitUntilComplete()
	i2.Connected().WaitUntilComplete()
	e.Connected().WaitUntilComplete()

	i1.Listen("SayHello")
	i2.Listen("SayHello")
	time.Sleep(1 * time.Second)

	params := []byte{4, 5, 63, 4}
	i1.Trigger("SayHello", params)

	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Didnt Got Request")
	case d := <-c:
		r := d.(*messages.Request)
		if r.Importer != i1.UUID() {
			t.Error("Wrong Import UUID", r.Importer, i1.UUID())
		}
		if !bytes.Equal(r.Parameter(), params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
		e.Reply(r, params)
	}

	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Didnt Got Result 1")
	case d := <-c1:
		r := d.(*messages.Result)
		if r.Exporter != e.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e.UUID())
		}
		if !bytes.Equal(r.Parameter(), params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
	}
	select {
	case <-time.After(2 * time.Second):
		t.Fatal("Didnt Got Result 2")
	case d := <-c2:
		r := d.(*messages.Result)
		if r.Exporter != e.UUID() {
			t.Error("Wrong Export UUID", r.Exporter, e.UUID())
		}
		if !bytes.Equal(r.Parameter(), params) {
			t.Error("Wrong Params", r.Parameter(), params)
		}
	}

}