Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when querying #65

Merged
merged 3 commits into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (a *Archive) Needed(n int) []string {
if n >= len(needed) {
return needed
}
return needed[len(a.chronological)-n:]
return needed[len(needed)-n:]
}

// Has returns whether the archive contains a message with the given ID.
Expand Down
32 changes: 32 additions & 0 deletions archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,35 @@ func TestNeeded(t *testing.T) {
}
}
}

// TestLongHistNeeded is a regression test that ensures that a very long message history with many unknown
// parents doesn't crash the client. (github.com/arborchat/muscadine/issues/61)
func TestLongHistNeeded(t *testing.T) {
g := gomega.NewGomegaWithT(t)
a := newOrSkip(t)
message := arbor.ChatMessage{
UUID: "whatever",
Parent: "something",
Content: "a lame test",
Timestamp: 500000,
Username: "Socrates",
}
// add lots of messages with known parents
for i := 0; i < 100; i++ {
message.Parent = message.UUID // make a child of the previous iteration's message
message.UUID += "a" // ensure new id each iteration
added := new(arbor.ChatMessage) // allocate new memory for message so all pointers don't go to same address
*added = message
addOrSkip(t, a, added)
}
// add ten messages with unknown parents
for i := 0; i < 10; i++ {
message.Parent += "b" // make a child of the previous iteration's message
message.UUID += "a" // ensure new id each iteration
added := new(arbor.ChatMessage) // allocate new memory for message so all pointers don't go to same address
*added = message
addOrSkip(t, a, added)
}
needed := a.Needed(5)
g.Expect(len(needed)).To(gomega.Equal(5))
}