Esempio n. 1
0
func TestDiskMailboxNackPutsAMessageAtTheFront(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	msg := vega.Msg("hello")
	msg2 := vega.Msg("2nd message")

	m.Push(msg)
	m.Push(msg2)

	out, _ := m.Poll()
	assert.True(t, msg.Equal(out), "wrong value")

	err = m.Nack(out.MessageId)
	if err != nil {
		panic(err)
	}

	out3, _ := m.Poll()
	assert.True(t, out.Equal(out3), "nack'd message did not come back")
}
Esempio n. 2
0
func TestDiskMailboxDiscontiniousNack(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	msg := vega.Msg("hello")
	msg2 := vega.Msg("2nd message")

	m.Push(msg)
	m.Push(msg2)

	out1, _ := m.Poll()
	assert.True(t, msg.Equal(out1), "wrong value")

	out2, _ := m.Poll()
	assert.True(t, msg2.Equal(out2), "wrong value")

	err = m.Nack(out1.MessageId)
	if err != nil {
		panic(err)
	}

	stats := m.Stats()
	assert.Equal(t, 1, stats.Size, "nack didn't change size")

	out3, _ := m.Poll()
	assert.True(t, out1.Equal(out3), "nack'd message did not come back")

	stats = m.Stats()
	assert.Equal(t, 2, stats.InFlight, "failed to track discontinious message")

	err = m.Ack(out3.MessageId)
	if err != nil {
		panic(err)
	}

	assert.Equal(t, 1, m.Stats().InFlight, "ack'd discontinous message failed")

	err = m.Ack(out2.MessageId)
	if err != nil {
		panic(err)
	}

	assert.Equal(t, 0, m.Stats().InFlight, "ack'd discontinous message failed")
}
Esempio n. 3
0
func TestDiskMailboxKeepsStatus(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	v, err := m.Poll()
	if err != nil {
		panic(err)
	}

	assert.Nil(t, v, "there shouldn't be anything in mailbox")

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	m.Poll()

	v, err = m.Poll()
	if err != nil {
		panic(err)
	}

	if v != nil {
		t.Fatal("there shouldn't be anything in the mailbox")
	}

	msg2 := vega.Msg([]byte("message 2"))
	msg3 := vega.Msg([]byte("third message"))

	m.Push(msg2)
	m.Push(msg3)

	ret, _ := m.Poll()
	assert.True(t, msg2.Equal(ret), "unable to pull correct message")

	ret, _ = m.Poll()
	assert.True(t, msg3.Equal(ret), "unable to pull correct message")

	ret, _ = m.Poll()
	assert.Nil(t, ret, "mailbox should be empty")
}
Esempio n. 4
0
func TestConsulNode(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn1, err := NewConsulClusterNode(
		&ConsulNodeConfig{
			AdvertiseAddr: "127.0.0.1",
			ListenPort:    8899,
			DataPath:      dir})

	if err != nil {
		panic(err)
	}

	defer cn1.Close()
	go cn1.Accept()

	dir2, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir2)

	cn2, err := NewConsulClusterNode(
		&ConsulNodeConfig{
			AdvertiseAddr: "127.0.0.1",
			ListenPort:    9900,
			DataPath:      dir2})

	if err != nil {
		panic(err)
	}

	defer cn2.Close()
	go cn2.Accept()

	cn1.Declare("a")

	// propagation delay
	time.Sleep(1000 * time.Millisecond)

	msg := vega.Msg([]byte("hello"))

	// debugf("pushing...\n")
	err = cn2.Push("a", msg)
	require.NoError(t, err)

	// debugf("polling...\n")
	got, err := cn1.Poll("a")
	if err != nil {
		panic(err)
	}

	assert.True(t, got.Message.Equal(msg), "didn't get the message")
}
Esempio n. 5
0
func TestConsulPusher(t *testing.T) {
	serv, err := vega.NewMemService(cPort)
	if err != nil {
		panic(err)
	}

	defer serv.Close()
	go serv.Accept()

	cp := &consulPusher{nil, cPort}

	cp.Connect()

	cp.Declare("a")

	payload := vega.Msg([]byte("hello"))

	cp.Push("a", payload)

	msg, err := cp.Poll("a")

	if msg == nil || !msg.Message.Equal(payload) {
		t.Fatal("couldn't talk to the service")
	}
}
Esempio n. 6
0
func TestClusterRoutes(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn, err := NewMemClusterNode(dir)
	if err != nil {
		panic(err)
	}

	defer cn.Close()

	memReg := vega.NewMemRegistry()
	memReg.Declare("a")

	cn.AddRoute("a", memReg)

	payload := vega.Msg([]byte("hello"))

	cn.Push("a", payload)

	msg, err := memReg.Poll("a")

	assert.Equal(t, payload, msg.Message, "message was not stored locally")
}
Esempio n. 7
0
func TestClusterLocalMessages(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn, err := NewMemClusterNode(dir)
	if err != nil {
		panic(err)
	}

	defer cn.Close()

	err = cn.Declare("a")
	if err != nil {
		panic(err)
	}

	payload := vega.Msg([]byte("hello"))

	err = cn.Push("a", payload)
	if err != nil {
		panic(err)
	}

	msg, err := cn.disk.Mailbox("a").Poll()

	assert.Equal(t, payload, msg, "message was not stored locally")
}
Esempio n. 8
0
func TestClusterAbandon(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn, err := NewMemClusterNode(dir)
	if err != nil {
		panic(err)
	}

	defer cn.Close()

	err = cn.Declare("a")
	if err != nil {
		panic(err)
	}

	payload := vega.Msg([]byte("hello"))

	err = cn.Push("a", payload)
	if err != nil {
		panic(err)
	}

	err = cn.Abandon("a")
	if err != nil {
		panic(err)
	}

	err = cn.Push("a", payload)
	assert.Error(t, err, "mailbox was not abandoned")
}
Esempio n. 9
0
func TestDiskMailboxPush(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	msg := vega.Msg([]byte("hello"))

	err = m.Push(msg)
	require.NoError(t, err)

	out, err := m.Poll()
	require.NoError(t, err)

	assert.True(t, out.Equal(msg), "wrong value")
}
Esempio n. 10
0
func TestClusterLongPoll(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn, err := NewMemClusterNode(dir)
	if err != nil {
		panic(err)
	}

	defer cn.Close()

	msg := vega.Msg([]byte("hello"))

	var got *vega.Delivery

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		got, _ = cn.LongPoll("a", 2*time.Second)
	}()

	cn.Declare("a")
	cn.Push("a", msg)

	wg.Wait()

	assert.Equal(t, msg, got.Message, "long poll didn't see the value")
}
Esempio n. 11
0
func TestDiskMailboxPersists(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	m := r.Mailbox("a")

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	r.Close()

	r2, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r2.Close()

	m2 := r2.Mailbox("a")

	ret, _ := m2.Poll()
	assert.True(t, msg.Equal(ret), "couldn't pull the message out")
}
Esempio n. 12
0
func TestDiskMailboxWatcher(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	watch := m.AddWatcher()

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	select {
	case ret := <-watch:
		assert.True(t, msg.Equal(ret), "wrong message")
	default:
		t.Fatal("watch didn't get value")
	}
}
Esempio n. 13
0
func TestDiskMailboxWatcherGoesInflight(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	watch := m.AddWatcher()

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	assert.Equal(t, 1, m.Stats().InFlight, "push did not set the message inflight")

	select {
	case ret := <-watch:
		err := m.Ack(ret.MessageId)
		assert.NoError(t, err, "wrong message")
	default:
		t.Fatal("watch didn't get value")
	}
}
Esempio n. 14
0
func TestConsulNodeRedeclaresOnStart(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn1, err := NewConsulClusterNode(
		&ConsulNodeConfig{
			AdvertiseAddr: "127.0.0.1",
			ListenPort:    8899,
			DataPath:      dir})

	if err != nil {
		panic(err)
	}

	// defer cn1.Close()
	go cn1.Accept()

	cl, err := vega.NewClient(":8899")

	cn1.Declare("a")

	// propagation delay
	time.Sleep(1000 * time.Millisecond)

	msg := vega.Msg([]byte("hello"))

	// debugf("pushing...\n")
	err = cl.Push("a", msg)
	if err != nil {
		panic(err)
	}

	cl.Close()
	cn1.Cleanup()
	cn1.Close()

	cn1, err = NewConsulClusterNode(
		&ConsulNodeConfig{
			AdvertiseAddr: "127.0.0.1",
			ListenPort:    8899,
			DataPath:      dir})

	if err != nil {
		panic(err)
	}

	defer cn1.Close()
	go cn1.Accept()

	cl, _ = vega.NewClient(":8899")

	err = cl.Push("a", msg)
	assert.NoError(t, err, "routes were not readded")
}
Esempio n. 15
0
func TestDiskMailboxAck(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	msg := vega.Msg([]byte("hello"))

	err = m.Push(msg)
	require.NoError(t, err)

	out, err := m.Poll()
	require.NoError(t, err)

	assert.True(t, out.Equal(msg), "wrong value")

	stats := m.Stats()

	assert.Equal(t, stats.Size, 0, "poll did not consume the message")
	assert.Equal(t, stats.InFlight, 1, "poll did not put the message inflight")

	err = m.Ack(out.MessageId)
	if err != nil {
		panic(err)
	}

	stats = m.Stats()

	assert.Equal(t, stats.InFlight, 0, "ack did not remove the inflight message")
}
Esempio n. 16
0
func TestDiskMailboxWatcherIsCancelable(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	m := r.Mailbox("a")

	done := make(chan struct{})

	watch := m.AddWatcherCancelable(done)

	close(done)

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	select {
	case ret := <-watch:
		assert.Nil(t, ret, "done didn't close indicator")
	default:
		t.Fatal("watch didn't get value")
	}

	out, err := m.Poll()
	if err != nil {
		panic(err)
	}

	assert.True(t, msg.Equal(out), "didn't get right message")
}
Esempio n. 17
0
func TestDiskMailboxNack(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	msg := vega.Msg([]byte("hello"))

	err = m.Push(msg)
	require.NoError(t, err)

	out, err := m.Poll()
	require.NoError(t, err)
	assert.True(t, out.Equal(msg), "wrong value")

	out2, err := m.Poll()
	require.NoError(t, err)
	assert.Nil(t, out2, "where did this message come from?")

	err = m.Nack(out.MessageId)
	if err != nil {
		panic(err)
	}

	out3, _ := m.Poll()
	assert.True(t, out.Equal(out3), "nack'd message did not come back")
}
Esempio n. 18
0
func TestDiskMailboxAbandon(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	m := r.Mailbox("a")

	err = m.Push(vega.Msg([]byte("hello")))
	if err != nil {
		panic(err)
	}

	var data []byte

	r.db.View(func(tx *bolt.Tx) error {
		data = tx.Bucket([]byte("a")).Get([]byte("m-0"))
		return nil
	})

	assert.NotEqual(t, 0, len(data), "mailbox not setup")

	m.Abandon()

	r.db.View(func(tx *bolt.Tx) error {
		buk := tx.Bucket([]byte("a"))
		assert.Nil(t, buk)
		return nil
	})
}
Esempio n. 19
0
func TestDiskMailboxStats(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	r, err := NewDiskStorage(dir)
	if err != nil {
		panic(err)
	}

	defer r.Close()

	m := r.Mailbox("a")

	if m.Stats().Size != 0 {
		t.Fatal("there shouldn't be anything in mailbox")
	}

	msg := vega.Msg([]byte("hello"))

	m.Push(msg)

	assert.Equal(t, 1, m.Stats().Size, "stats not updated to 1")

	m.Push(msg)

	assert.Equal(t, 2, m.Stats().Size, "stats not updated to 2")

	m.Poll()
	m.Poll()

	assert.Equal(t, 0, m.Stats().Size, "stats not updated to 0")
}
Esempio n. 20
0
func TestClusterRoutesViaNetwork(t *testing.T) {
	dir, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir)

	cn, err := NewMemClusterNode(dir)
	if err != nil {
		panic(err)
	}

	defer cn.Close()

	dir2, err := ioutil.TempDir("", "mailbox")
	if err != nil {
		panic(err)
	}

	defer os.RemoveAll(dir2)

	cn2, err := NewMemClusterNode(dir2)
	if err != nil {
		panic(err)
	}

	defer cn2.Close()

	// Setup 2 service objects

	s1, err := vega.NewService(cPort, cn)
	if err != nil {
		panic(err)
	}

	defer s1.Close()
	go s1.Accept()

	s2, err := vega.NewService(cPort2, cn2)
	if err != nil {
		panic(err)
	}

	defer s2.Close()
	go s2.Accept()

	// Wire up a client going to s1

	toS1, err := vega.NewClient(cPort)
	if err != nil {
		panic(err)
	}

	toS1.Declare("a")
	cn2.AddRoute("a", toS1)

	// Push data into cn2 and see it show up in cn

	toS2, err := vega.NewClient(cPort2)
	if err != nil {
		panic(err)
	}

	msg := vega.Msg([]byte("between nodes"))

	err = toS2.Push("a", msg)
	if err != nil {
		panic(err)
	}

	// debugf("polling\n")

	ret, err := toS1.Poll("a")
	if err != nil {
		panic(err)
	}

	assert.True(t, msg.Equal(ret.Message), "message did not route properly")
}