Ejemplo n.º 1
0
func TestEmptySubscription(test *testing.T) {
	node := subscription.NewNode()
	if node.Len() != 0 {
		test.Log("empty subscripition should be empty")
		test.FailNow()
	}
}
Ejemplo n.º 2
0
func BenchmarkMatch(b *testing.B) {
	node := subscription.NewNode()
	keys := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
	node.Add(keys)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		node.Match(keys)
	}
}
Ejemplo n.º 3
0
// Connect attempts to connect to a running mist server at the clients specified
// host and port.
func NewRemoteClient(address string) (Client, error) {
	client := &remoteSubscriber{
		subscriptions: subscription.NewNode(),
		done:          make(chan error),
		waiting:       make([]chan remoteReply, 0),
		data:          make(chan Message),
		open:          true,
		conn:          nothing{},
	}
	go client.loop(address)
	return client, nil
}
Ejemplo n.º 4
0
func EnableReplication(server *mist.Mist, discover discovery.Discover) *replicate {

	replicate := &replicate{
		mist:          server,
		newClient:     make(chan mist.Client),
		doneClient:    make(chan mist.Client),
		subscriptions: subscription.NewNode(),
	}

	discover.Handle("mist", replicate)

	return replicate
}
Ejemplo n.º 5
0
func TestList(test *testing.T) {
	node := subscription.NewNode()
	node.Add([]string{"a", "b"})
	list := node.ToSlice()
	if len(list) != 1 {
		test.Log("wrong length of list", list)
		test.FailNow()
	}

	if len(list[0]) != 2 {
		test.Log("wrong numer of tags", list[0], len(list[0]))
		test.FailNow()
	}
}
Ejemplo n.º 6
0
func TestAddRemove(test *testing.T) {
	node := subscription.NewNode()
	node.Add([]string{"a", "b"})
	if node.Len() != 1 {
		test.Log("add should have added a node")
		test.FailNow()
	}

	node.Remove([]string{"a", "b"})
	if node.Len() != 0 {
		test.Log("remove should have removed a node")
		test.FailNow()
	}
}
Ejemplo n.º 7
0
func TestMatches(test *testing.T) {
	matches := []matchTest{
		{
			[]string{},
			[]subTest{
				{[]string{"a"}, false},
				{[]string{}, false},
			},
		},
		{
			[]string{"a", "b"},
			[]subTest{
				{[]string{}, false},
				{[]string{"a", "b"}, true},
				{[]string{"b", "a"}, true},
				{[]string{"b", "a", "c"}, true},
				{[]string{"c", "a", "b"}, true},
				{[]string{"c", "a"}, false},
				{[]string{"c", "b"}, false},
				{[]string{"c"}, false},
			},
		},
	}

	for _, match := range matches {
		node := subscription.NewNode()
		sort.Sort(sort.StringSlice(match.sub))
		node.Add(match.sub)

		for _, t := range match.tests {
			sort.Sort(sort.StringSlice(t.test))
			if node.Match(t.test) != t.result {
				test.Logf("match failed: %v:%v expected %v", match.sub, t.test, t.result)
				test.Fail()
			}
		}
		node.Remove(match.sub)
		for _, t := range match.tests {
			if node.Match(t.test) {
				test.Logf("match failed: []:%v expected false", t.test)
				test.Fail()
			}
		}

	}
}
Ejemplo n.º 8
0
Archivo: local.go Proyecto: jshawl/mist
func NewLocalClient(mist *Mist, buffer int) Client {
	client := &localSubscriber{
		check:         make(chan Message, buffer),
		done:          make(chan bool),
		pipe:          make(chan Message),
		mist:          mist,
		id:            mist.nextId(),
		subscriptions: subscription.NewNode(),
		internal:      false,
	}

	// this gofunc handles matching messages to subscriptions for the client
	go func(client *localSubscriber) {

		defer func() {
			close(client.check)
			close(client.pipe)
		}()

		for {
			select {
			case msg := <-client.check:

				switch {
				case msg.internal && client.internal:
					client.pipe <- msg
				default:
					client.Lock()
					match := client.subscriptions.Match(msg.Tags)
					client.Unlock()

					if match {
						client.pipe <- msg
					}
				}
			case <-client.done:
				return
			}
		}
	}(client)

	// add the local client to mists list of subscribers
	mist.subscribers[client.id] = client

	return client
}
Ejemplo n.º 9
0
func TestAddRemoveSimilarDuplicate(test *testing.T) {
	node := subscription.NewNode()
	node.Add([]string{"a", "b"})
	if node.Len() != 1 {
		test.Log("similar duplicate add should have added a node")
		test.FailNow()
	}
	node.Add([]string{"a", "b", "c"})
	if node.Len() != 2 {
		test.Log("similar duplicate add should not have added a node")
		test.FailNow()
	}

	node.Remove([]string{"a", "b"})
	if node.Len() != 1 {
		test.Log("similar duplicate remove should not have removed a node with a count > 1")
		test.FailNow()
	}
	node.Remove([]string{"a", "b", "c"})
	if node.Len() != 0 {
		test.Log("similar duplicate remove should have removed a node", node.Len())
		test.FailNow()
	}
}