Skip to content

Commit

Permalink
more fork tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Dec 16, 2024
1 parent 25be0e6 commit b3c4b22
Show file tree
Hide file tree
Showing 2 changed files with 1,599 additions and 1,463 deletions.
136 changes: 136 additions & 0 deletions example/src/tests/clientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,142 @@ test('groups cannot fork short version', async () => {
return true
})

test('groups cannot fork short version - add members', async () => {
const [alix, bo, new_one, new_two, new_three, new_four] = await createClients(6)
// Create group with 2 users
const alixGroup = await alix.conversations.newGroup([
bo.address,
new_one.address,
new_two.address,
])

// sync clients
await alix.conversations.sync()
await bo.conversations.sync()
const boGroup: Group<DefaultContentTypes> = (await bo.conversations.findGroup(alixGroup.id))!

// Remove two members in parallel
// NB => if we don't use Promise.all but a loop, we don't get a fork
console.log('*************libxmtp*********************: Adding members in parallel')
await Promise.all([
alixGroup.addMembers([new_three.address]),
alixGroup.addMembers([new_four.address])
])

// Helper to send a message from a bunch of senders and make sure it is received by all receivers
const testMessageSending = async (senderGroup: Group<DefaultContentTypes>, receiverGroup: Group<DefaultContentTypes>) => {
const messageContent = Math.random().toString(36)
await senderGroup.sync()
await alixGroup.send(messageContent)

await delayToPropogate(500)
await alixGroup.sync()
await receiverGroup.sync()

const messages = await receiverGroup.messages({
direction: 'DESCENDING',
})
const lastMessage = messages[0]
console.log(
`${receiverGroup.client.address} sees ${messages.length} messages in group`
)
assert(
lastMessage !== undefined &&
lastMessage.nativeContent.text === messageContent,
`${receiverGroup.client.address} should have received the message, FORK? ${lastMessage?.nativeContent.text} !== ${messageContent}`
)
}
// When forked, it stays forked even if we try 5 times
// but sometimes it is not forked and works 5/5 times
let forkCount = 0
const tryCount = 5
for (let i = 0; i < tryCount; i++) {
console.log(`Checking fork status ${i+1}/${tryCount}`)
try {
await alixGroup.sync()
await boGroup.sync()
await delayToPropogate(500)
await testMessageSending(alixGroup, boGroup)
console.log('Not forked!')
} catch (e: any) {
console.log('Forked!')
console.log(e)
forkCount++
}
}
assert(forkCount === 0, `Forked ${forkCount}/${tryCount} times`)

return true
})

test('groups cannot fork short version - update metadata', async () => {
const [alix, bo, new_one, new_two, new_three, new_four] = await createClients(6)
// Create group with 2 users
const alixGroup = await alix.conversations.newGroup([
bo.address,
new_one.address,
new_two.address,
])

// sync clients
await alix.conversations.sync()
await bo.conversations.sync()
const boGroup: Group<DefaultContentTypes> = (await bo.conversations.findGroup(alixGroup.id))!

// Remove two members in parallel
// NB => if we don't use Promise.all but a loop, we don't get a fork
console.log('*************libxmtp*********************: Updating metadata in parallel')
await Promise.all([
alixGroup.updateGroupName('new name'),
alixGroup.updateGroupName('new name 2')
])

// Helper to send a message from a bunch of senders and make sure it is received by all receivers
const testMessageSending = async (senderGroup: Group<DefaultContentTypes>, receiverGroup: Group<DefaultContentTypes>) => {
const messageContent = Math.random().toString(36)
await senderGroup.sync()
await alixGroup.send(messageContent)

await delayToPropogate(500)
await alixGroup.sync()
await receiverGroup.sync()

const messages = await receiverGroup.messages({
direction: 'DESCENDING',
})
const lastMessage = messages[0]
console.log(
`${receiverGroup.client.address} sees ${messages.length} messages in group`
)
assert(
lastMessage !== undefined &&
lastMessage.nativeContent.text === messageContent,
`${receiverGroup.client.address} should have received the message, FORK? ${lastMessage?.nativeContent.text} !== ${messageContent}`
)
}
// When forked, it stays forked even if we try 5 times
// but sometimes it is not forked and works 5/5 times
let forkCount = 0
const tryCount = 5
for (let i = 0; i < tryCount; i++) {
console.log(`Checking fork status ${i+1}/${tryCount}`)
try {
await alixGroup.sync()
await boGroup.sync()
await delayToPropogate(500)
await testMessageSending(alixGroup, boGroup)
console.log('Not forked!')
} catch (e: any) {
console.log('Forked!')
console.log(e)
forkCount++
}
}
assert(forkCount === 0, `Forked ${forkCount}/${tryCount} times`)

return true
})

// test('can make a client', async () => {
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// const keyBytes = new Uint8Array([
Expand Down
Loading

0 comments on commit b3c4b22

Please sign in to comment.