Exemple #1
0
func TestContactsPush(t *testing.T) {
	contactToAdd := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contacts := kademlia.Contacts{}

	contacts.Push(contactToAdd)

	if contacts.Len() != 1 {
		t.Error("Contact was not pushed to Contacts array")
	}
	if contacts[0].ID != contactToAdd.ID || contacts[0].Address != contactToAdd.Address {
		t.Error("Contact not copied correctly during Push")
	}
}
Exemple #2
0
func TestContactsPop(t *testing.T) {
	contactToRemove := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contacts := kademlia.Contacts{contactToRemove}

	removedContact := contacts.Pop().(kademlia.Contact)

	if contacts.Len() != 0 {
		t.Error("Contact was not popped from Contacts array")
	}
	if contactToRemove.ID != removedContact.ID || contactToRemove.Address != removedContact.Address {
		t.Error("Contact was not copied correctly during Pop")
	}
}
Exemple #3
0
func TestContactsLen(t *testing.T) {
	expectedLen := 20
	contacts := kademlia.Contacts{}

	for i := 0; i < expectedLen; i++ {
		if contacts.Len() != i {
			t.Error(fmt.Sprintf("Contacts length should be %d, got %d", i, contacts.Len()))
		}

		id := kademlia.NewRandomNodeID()
		address := fmt.Sprintf("127.0.0.1:60%d", i)
		contacts = append(contacts, kademlia.NewContact(id, address))
	}

	if contacts.Len() != expectedLen {
		t.Error(fmt.Sprintf("Contacts length should be %d, got %d", expectedLen, contacts.Len()))
	}
}