Skip to content

Commit dd05280

Browse files
authored
Merge branch 'master' into add-persistent-dir
2 parents 7208191 + d1353fb commit dd05280

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

src/codeu/chat/relay/Server.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,29 +211,23 @@ public Collection<Relay.Bundle> read(Uuid teamId, byte[] teamSecret, Uuid root,
211211

212212
if (authenticate(teamId, teamSecret)) {
213213

214-
int remaining = Math.min(range, maxRead);
215-
216214
LOG.info(
217215
"Request to read from server requested=%d allowed=%d",
218216
range,
219217
maxRead);
220218

221-
// Writing is a one way flag (once set it will not be unset) that switches
222-
// between looking for the starting message and writing all messages to the
223-
// output.
224-
boolean writing = root == Uuid.NULL || root == null;
225-
226219
for (final Relay.Bundle message : history) {
227220

228-
if (writing && remaining > 0) {
221+
// Only add a message if there is room. We cannot stop
222+
// searching in case we see the root later on.
223+
if (found.size() < Math.min(range, maxRead)) {
229224
found.add(message);
230225
}
231226

232-
remaining = Math.max(remaining - 1, 0);
233-
234-
// Only update "writing" after the check as the root is already known and
235-
// should not be included in the output.
236-
writing |= Uuid.equals(root, message.id());
227+
// If the start is found, drop all previous messages.
228+
if (message.id().equals(root)) {
229+
found.clear();
230+
}
237231
}
238232

239233
LOG.info(

test/codeu/chat/relay/ServerTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,76 @@ public void testHistoryOverwrite() {
214214
assertTrue(Uuid.equals(bundle.message().id(), new Uuid(7)));
215215
}
216216
}
217+
218+
@Test
219+
public void testReadWithMissingRoot() {
220+
221+
final Server relay = new Server(8, 8);
222+
223+
final Uuid team = new Uuid(3);
224+
final byte[] secret = { 0x00, 0x01, 0x02 };
225+
226+
assertTrue(relay.addTeam(team, secret));
227+
228+
assertTrue(relay.write(team,
229+
secret,
230+
relay.pack(new Uuid(4), "User", Time.now()),
231+
relay.pack(new Uuid(5), "Conversation", Time.now()),
232+
relay.pack(new Uuid(6), "Hello World", Time.now())));
233+
234+
final Collection<Relay.Bundle> read = relay.read(team, secret, new Uuid(7), 1);
235+
assertTrue(read.size() == 1);
236+
237+
// By the assertion above this loop should only execute once as there should only
238+
// be a single value in the collection.
239+
240+
for (final Relay.Bundle bundle : read) {
241+
assertTrue(Uuid.equals(bundle.team(), team));
242+
assertTrue(Uuid.equals(bundle.user().id(), new Uuid(4)));
243+
assertTrue(Uuid.equals(bundle.conversation().id(), new Uuid(5)));
244+
assertTrue(Uuid.equals(bundle.message().id(), new Uuid(6)));
245+
}
246+
}
247+
248+
@Test
249+
public void testReadMidHistory() {
250+
251+
final Server relay = new Server(8, 8);
252+
253+
final Uuid team = new Uuid(3);
254+
final byte[] secret = { 0x00, 0x01, 0x02 };
255+
256+
assertTrue(relay.addTeam(team, secret));
257+
258+
assertTrue(relay.write(team,
259+
secret,
260+
relay.pack(new Uuid(4), "User", Time.now()),
261+
relay.pack(new Uuid(5), "Conversation", Time.now()),
262+
relay.pack(new Uuid(6), "Hello World", Time.now())));
263+
264+
assertTrue(relay.write(team,
265+
secret,
266+
relay.pack(new Uuid(7), "User", Time.now()),
267+
relay.pack(new Uuid(8), "Conversation", Time.now()),
268+
relay.pack(new Uuid(9), "Hello World", Time.now())));
269+
270+
assertTrue(relay.write(team,
271+
secret,
272+
relay.pack(new Uuid(10), "User", Time.now()),
273+
relay.pack(new Uuid(11), "Conversation", Time.now()),
274+
relay.pack(new Uuid(12), "Hello World", Time.now())));
275+
276+
final Collection<Relay.Bundle> read = relay.read(team, secret, new Uuid(2), 1);
277+
assertTrue(read.size() == 1);
278+
279+
// By the assertion above this loop should only execute once as there should only
280+
// be a single value in the collection.
281+
282+
for (final Relay.Bundle bundle : read) {
283+
284+
// The relay server uses a linear id generator starting at 1 - so starting
285+
// bundle 2, the id should be 3.
286+
assertTrue(Uuid.equals(bundle.id(), new Uuid(3)));
287+
}
288+
}
217289
}

0 commit comments

Comments
 (0)