예제 #1
0
func TestWorkerReadyMessage(t *testing.T) {
	broker := fmt.Sprintf("inproc://%s", randomHex())
	service := randomHex()

	sock, err := czmq.NewRouter(broker)
	if err != nil {
		t.Errorf("NewRouter(%v) failed", broker)
		return
	}

	go func(broker, service string) {
		NewWorker(broker, service).ensure_connected()
	}(broker, service)

	// Worker should send READY as soon as it connects
	frames, err := sock.RecvMessage()
	if err != nil {
		t.Errorf("%s", err)
		return
	}

	// Skip the first frame since that's the DEALER/ROUTER id
	err = validateWorkerReady(frames[1:], service)
	if err != nil {
		t.Errorf("%s", err)
		return
	}
}
예제 #2
0
func TestWorkerRun(t *testing.T) {
	broker := fmt.Sprintf("inproc://%s", randomHex())
	service := randomHex()
	client := randomHex()

	sock, err := czmq.NewRouter(broker)
	if err != nil {
		t.Errorf("NewRouter(%v) failed", broker)
		return
	}

	go func(broker, service string) {
		NewWorker(broker, service).Run(&helloGreeter{})
	}(broker, service)

	frames, _ := sock.RecvMessage()
	err = validateWorkerReady(frames[1:], service)
	if err != nil {
		t.Errorf("Error reading READY message: %s\n", err)
		return
	}

	err = sock.SendMessage([][]byte{
		frames[0], // router/dealer ID
		[]byte{},
		mdp_WORKER,
		[]byte{byte(c_REQUEST)},
		[]byte(client),
		[]byte{}, // SPEC Frame 4: Empty (zero bytes, envelope delimiter)
		[]byte("hello world"),
	})
	reply, err := sock.RecvMessage()
	if err != nil {
		t.Errorf("Error receiving reply: %s\n", err)
	}
	err = validateWorkerReply(reply[1:], []byte(client)) // [1:] to skip the router/dealer id
	if err != nil {
		t.Errorf("Reply validation failed: %s\n", err)
	}
}
예제 #3
0
func TestClientSendFraming(t *testing.T) {
	// Randomize things for better testing confidence
	endpoint := fmt.Sprintf("inproc://%s", randomHex())
	service := randomHex()
	payload := [1][]byte{[]byte(randomHex())}

	router, err := czmq.NewRouter(endpoint)
	defer router.Destroy()
	if err != nil {
		t.Errorf("Creating new router failed, %s: %s", endpoint, err)
		return
	}

	client := NewClient(endpoint)
	defer client.Destroy()
	go client.SendRecv(service, payload[:])

	frames, err := router.RecvMessage()
	if err != nil {
		t.Errorf("Error while reading from router %s: %s", endpoint, err)
		return
	}

	if count := len(frames); count < 5 {
		t.Errorf("Majordomo requests must have at least 5 frames, got %d frames\n", count)
		return
	}

	//for i, x := range frames { fmt.Printf("%d: %v (%s) \n", i, x, string(x)) }

	// frames[0] is the client/session id for the router socket, ignore it.
	// frames[1 ... ] are the actual request

	// Frame 0: Empty (zero bytes, invisible to REQ application)
	if len(frames[1]) != 0 {
		t.Errorf("Majordomo request frame #1 must be empty.\n")
		return
	}

	if !bytes.Equal(frames[2], mdp_CLIENT) {
		t.Errorf("Majordomo request frame #2 must be `%s`, got `%s`\n", string(mdp_CLIENT), string(frames[1]))
		return
	}

	//if len(frames[3]) != 1 || command(frames[3][0]) != c_REQUEST {
	//t.Errorf("Majordomo request frame #3 must be REQUEST")
	//return
	//}

	if !bytes.Equal(frames[3], []byte(service)) {
		t.Errorf("Majordomo request frame #4 must be a service name. Expected `%s`, got `%s`", service, string(frames[4]))
		return
	}

	if expected, actual := len(payload), len(frames[4:]); expected != actual {
		t.Errorf("Expected body with %d frames, got %d frames\n", expected, actual)
		return
	}

	if !bytes.Equal(frames[4], payload[0]) {
		t.Errorf("Majordomo request body did not match.")
		return
	}
}