Ejemplo n.º 1
0
func TestJsonSerializer(t *testing.T) {
	node := n.Node{Host: "localhost", Port: 13123}

	msg := m.Hello{
		Id:      m.NewId(),
		From:    node,
		Details: map[string]interface{}{"foo": "bar"},
	}

	s := &JsonSerializer{}
	data, err := s.Serialize(msg)
	if err != nil {
		t.Error("Unexpected error serializing ", err)
	}

	var rcvMessage m.Message
	rcvMessage, err = s.Deserialize(data)
	if err != nil {
		t.Error("Unexpected error deserializing ", err)
	}

	switch rcvMessage.(type) {
	case *m.Hello:
		h := rcvMessage.(*m.Hello)
		if msg.Id != h.Id {
			t.Error("Message Ids don't match", msg.Id, h.Id)
		}

		if "bar" != h.Details["foo"] {
			t.Error("Message Payload don't match")
		}
	default:
		t.Error("Wrong type")
	}
}
Ejemplo n.º 2
0
func (p *Peer) SayHello() (u message.ID, err error) {
	msg := message.Hello{
		Id:      message.NewId(),
		From:    p.from,
		Details: map[string]interface{}{"foo": "bar"},
	}
	p.Commit(msg)

	return msg.Id, err
}
Ejemplo n.º 3
0
func TestBasicNopPeerTest(t *testing.T) {
	ch := make(chan message.Message, 10)
	fkc := &NopPeer{"localhost", 9000, ch}

	msg := message.Hello{
		Id:      message.NewId(),
		From:    node.Node{"localhost", 9000},
		Details: map[string]interface{}{"foo": "bar"},
	}

	fkc.Send(msg)

	rcvMsg := <-fkc.ReceiveChan()
	if !reflect.DeepEqual(msg, rcvMsg) {
		t.Errorf("Expected %s, got %s", msg, rcvMsg)
	}
}
Ejemplo n.º 4
0
func TestBasicLinksOnServerClient(t *testing.T) {
	go startTestServer()
	time.Sleep(time.Second * 1)

	conn, err := net.Dial("tcp", "localhost:8002")
	if err != nil {
		fmt.Println("dial error:", err)
		return
	}
	defer conn.Close()

	linkA := NewJSONSocketLink(conn)

	id := message.NewId()
	msg := message.Hello{
		Id:      id,
		Details: map[string]interface{}{"foo": "bar"},
	}
	linkA.Send(msg)

	msgA, err := linkA.Receive()
	if err != nil {
		t.Error("Error on received Hello Message ", err)
	}

	fmt.Println("Message is ", msgA, "err", err)
	if msgA.MessageType() != 0 {
		t.Error("Error on received Hello Message ", msgA)
	}

	newMsg := msgA.(*message.Hello)
	if newMsg.Id != id {
		t.Error("Error on received Hello Message ", msgA)
	}

	if newMsg.Details["foo"].(string) != "bar" {
		t.Error("Error on received Hello Message ", msgA)
	}
}
Ejemplo n.º 5
0
func (ph *testLinkHandler) handleLink(p *SocketLink, from node.Node) {
	defer func() {
		ph.outChan <- ph.lastMsg
	}()

	for {
		select {
		case <-ph.exit:
			return
		default:
			msg, err := p.Receive()
			if err != nil {
				fmt.Println("Error Receiving on server, err ", err)
				return
			}

			if msg.MessageType() != 0 {
				fmt.Println("Error on received Hello Message ", msg)
				return
			}

			newMsg := msg.(*message.Hello)
			if newMsg.Details["foo"].(string) == "PONG" {
				newMsg.Details["foo"] = "PING"
			} else {
				newMsg.Details["foo"] = "PONG"
			}
			newMsg.Id = message.NewId()
			newMsg.From = from

			err = p.Send(newMsg)
			if err != nil {
				//fmt.Println("Error sending: ", err)
				return
			}
			ph.lastMsg = newMsg
		}
	}
}
Ejemplo n.º 6
0
func TestLinksOnPipes(t *testing.T) {
	a, b := net.Pipe()

	linkA := NewJSONSocketLink(a)
	linkB := NewJSONSocketLink(b)
	defer linkA.Conn.Close()
	defer linkB.Conn.Close()

	tp := &testLinkHandler{
		outChan: make(chan *message.Hello, 1),
		exit:    make(chan bool, 1),
	}
	go tp.handleLink(linkA, node.Node{Host: "localhost", Port: 5000})
	go tp.handleLink(linkB, node.Node{Host: "localhost", Port: 5005})

	id := message.NewId()
	//first message
	msg := message.Hello{
		Id:      id,
		From:    node.Node{Host: "localhost", Port: 5000},
		Details: map[string]interface{}{"foo": "bar"},
	}
	linkA.Send(msg)

	time.Sleep(time.Millisecond * 300)
	tp.exit <- true
	lastMessage := <-tp.outChan
	fmt.Println("lastMessage A", lastMessage)

	if lastMessage.Id == id {
		t.Error("LastMessage must be bigger ")
	}

	if lastMessage.Details["foo"].(string) != "PING" && lastMessage.Details["foo"].(string) != "PONG" {
		t.Error("LastMessageDetails has not changed! ")
	}
}
Ejemplo n.º 7
0
func TestPeerMessagingUnderPipes(t *testing.T) {
	a, b := net.Pipe()

	c1 := &Peer{
		Link:         NewJSONSocketLink(a),
		from:         node.Node{Host: "foo"},
		to:           node.Node{Host: "bar"},
		dataChan:     make(chan message.Message, 10),
		sendChan:     make(chan message.Message, 10),
		messageChan:  make(chan message.Message, 10),
		exitChan:     make(chan bool),
		doneChan:     make(chan bool),
		rstWatchChan: make(chan bool, 100),
		mode:         "pipe",
	}
	go c1.Run()

	c1Mirror := &Peer{
		Link:         NewJSONSocketLink(b),
		from:         node.Node{Host: "bar"},
		to:           node.Node{Host: "foo"},
		dataChan:     make(chan message.Message, 10),
		sendChan:     make(chan message.Message, 10),
		messageChan:  make(chan message.Message, 10),
		exitChan:     make(chan bool),
		doneChan:     make(chan bool),
		rstWatchChan: make(chan bool, 100),
		mode:         "pipe",
	}
	go c1Mirror.Run()

	var wg sync.WaitGroup

	resChan := make(chan message.Message, 2)
	doneChan := make(chan struct{})
	wg.Add(1)
	go func() {
		for {
			select {
			case r := <-c1.ReceiveChan():
				msg := r.(*message.Hello)
				msg.Id = message.NewId()
				resChan <- msg
			case r := <-c1Mirror.ReceiveChan():
				msg := r.(*message.Hello)
				msg.Id = message.NewId()
				resChan <- msg
			case <-c1.ResetWatcherChan():
				continue
			case <-c1Mirror.ResetWatcherChan():
				continue
			case <-doneChan:
				close(resChan)
				wg.Done()
				return
			}
		}
		return
	}()

	c1.SayHello()
	c1Mirror.SayHello()

	time.Sleep(time.Millisecond * 100)

	close(doneChan)
	wg.Wait()
	r := make([]message.Message, 0)
	for k := range resChan {
		r = append(r, k)
	}
	if len(r) != 2 {
		t.Error("Unexpected response size", r)
		t.Fail()
	}
	h1, ok := r[0].(*message.Hello)
	if !ok {
		t.Error("Error Casting to Hello ", h1)
	}

	h2, ok := r[1].(*message.Hello)
	if !ok {
		t.Error("Error Casting to Hello ", h2)
	}

	c1.Exit()
	c1Mirror.Exit()
}
Ejemplo n.º 8
0
func TestBasicFlowOnCoordinator(t *testing.T) {
	c := Start()
	c.Send(&message.Hello{Id: message.NewId()})
	c.Send(&message.Welcome{Id: message.NewId()})
	c.Send(&message.Abort{Id: message.NewId()})
}
Ejemplo n.º 9
0
func TestRouterAccept(t *testing.T) {
	r := &defaultRouter{
		handlers:        make(map[message.MsgType]handler.Handler),
		exit:            make(chan bool, 1),
		requestListener: watch.NewRequestListener(),
	}

	hello := &message.Hello{}
	ping := &message.Ping{}
	r.RegisterHandler(hello.MessageType(), fakeHelloHandler)
	r.RegisterHandler(ping.MessageType(), fakePingHandler)

	nodeA := node.Node{Host: "A", Port: 1}
	nodeB := node.Node{Host: "B", Port: 2}
	a, b := net.Pipe()

	c1 := peer.NewAcceptor(a, nodeA)
	c1.Identify(nodeB)
	go c1.Run()

	c1Mirror := peer.NewAcceptor(b, nodeB)
	c1Mirror.Identify(nodeA)
	go c1Mirror.Run()

	go func() {
		for {
			select {
			case <-c1.ResetWatcherChan():
				continue
			case <-c1Mirror.ResetWatcherChan():
				continue
			}
		}
	}()

	r.Accept(c1)

	c1Mirror.SayHello()
	result := <-c1Mirror.ReceiveChan()

	if result.MessageType() != 1 {
		t.Error("Unexpected response type, expected 1 got", result.MessageType())
	}

	id := message.NewId()
	msg := message.Ping{
		Id:   id,
		From: nodeA,
		To:   nodeB,
	}
	c1Mirror.Send(msg)

	result = <-c1Mirror.ReceiveChan()
	if result.MessageType() != 4 {
		t.Error("Unexpected response type, expected 1 got", result.MessageType())
	}

	pong := result.(*message.Pong)
	if pong.Id != id {
		t.Error("Unexpected result Id")
	}
	if pong.From != nodeB {
		t.Error("Unexpected result From")
	}

	r.Exit()
	c1.Exit()
	c1Mirror.Exit()
}