Merge branch 'sending-fix-storage' into 'master'

[Sending] Fix storage for sending events and add more tests.

See merge request famedly/famedlysdk!19
This commit is contained in:
Marcel 2019-06-26 18:03:20 +00:00
commit bb7a72fbf5
2 changed files with 34 additions and 20 deletions

View file

@ -204,27 +204,17 @@ class Room {
// On error, set status to -1 // On error, set status to -1
eventUpdate.content["status"] = -1; eventUpdate.content["status"] = -1;
client.connection.onEvent.add(eventUpdate); client.connection.onEvent.add(eventUpdate);
client.store?.db await client.store?.transaction(() {
?.rawUpdate("UPDATE Events SET status=-1 WHERE id=?", [messageID]); client.store.storeEventUpdate(eventUpdate);
});
} else { } else {
final String newEventID = res["event_id"]; final String newEventID = res["event_id"];
eventUpdate.content["status"] = 1; eventUpdate.content["status"] = 1;
eventUpdate.content["id"] = newEventID; eventUpdate.content["id"] = newEventID;
client.connection.onEvent.add(eventUpdate); client.connection.onEvent.add(eventUpdate);
await client.store?.transaction(() {
// Store the result in database client.store.storeEventUpdate(eventUpdate);
if (client.store != null) { });
final List<Map<String, dynamic>> eventQuery = await client.store.db
.rawQuery("SELECT * FROM Events WHERE id=?", [newEventID]);
if (eventQuery.length > 0) {
client.store.db
.rawDelete("DELETE FROM Events WHERE id=?", [messageID]);
} else {
client.store.db.rawUpdate(
"UPDATE Events SET id=?, status=1 WHERE id=?",
[newEventID, messageID]);
}
}
return newEventID; return newEventID;
} }
return null; return null;

View file

@ -85,6 +85,7 @@ void main() {
expect(updateCount, 2); expect(updateCount, 2);
expect(insertList, [0, 0]); expect(insertList, [0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events.length, 2); expect(timeline.events.length, 2);
expect(timeline.events[0].id, "1"); expect(timeline.events[0].id, "1");
expect(timeline.events[0].sender.id, "@alice:example.com"); expect(timeline.events[0].sender.id, "@alice:example.com");
@ -101,6 +102,7 @@ void main() {
expect(updateCount, 4); expect(updateCount, 4);
expect(insertList, [0, 0, 0]); expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events[0].content["txid"], "1234"); expect(timeline.events[0].content["txid"], "1234");
expect(timeline.events[0].id, "42"); expect(timeline.events[0].id, "42");
expect(timeline.events[0].status, 1); expect(timeline.events[0].status, 1);
@ -123,19 +125,41 @@ void main() {
expect(updateCount, 5); expect(updateCount, 5);
expect(insertList, [0, 0, 0]); expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events[0].id, "42"); expect(timeline.events[0].id, "42");
expect(timeline.events[0].status, 2); expect(timeline.events[0].status, 2);
}); });
test("Send message with error", () async { test("Send message with error", () async {
client.connection.onEvent.add(EventUpdate(
type: "timeline",
roomID: roomID,
eventType: "m.room.message",
content: {
"type": "m.room.message",
"content": {"msgtype": "m.text", "body": "Testcase"},
"sender": "@alice:example.com",
"status": 0,
"id": "abc",
"origin_server_ts": testTimeStamp
}));
await new Future.delayed(new Duration(milliseconds: 50));
room.sendTextEvent("test", txid: "errortxid"); room.sendTextEvent("test", txid: "errortxid");
await new Future.delayed(new Duration(milliseconds: 50));
room.sendTextEvent("test", txid: "errortxid2");
await new Future.delayed(new Duration(milliseconds: 50));
room.sendTextEvent("test", txid: "errortxid3");
await new Future.delayed(new Duration(milliseconds: 50)); await new Future.delayed(new Duration(milliseconds: 50));
expect(updateCount, 7); expect(updateCount, 12);
expect(insertList, [0, 0, 0, 0]); expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
expect(timeline.events[0].content["txid"], "errortxid"); expect(insertList.length, timeline.events.length);
expect(timeline.events[0].content["txid"], "errortxid3");
expect(timeline.events[0].status, -1); expect(timeline.events[0].status, -1);
expect(timeline.events[1].content["txid"], "errortxid2");
expect(timeline.events[1].status, -1);
expect(timeline.events[2].content["txid"], "errortxid");
expect(timeline.events[2].status, -1);
}); });
}); });
} }