Skip to content

Commit

Permalink
Merge pull request #65 from arborchat/issue61
Browse files Browse the repository at this point in the history
Fix panic when querying
  • Loading branch information
whereswaldon authored Jan 1, 2019
2 parents 5a132da + 36f80d4 commit 91514de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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))
}

0 comments on commit 91514de

Please sign in to comment.