Store, Sync, main: a deleted text stays deleted
imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
This commit is contained in:
parent
07c6c3e5bd
commit
53bc4797bc
5 changed files with 242 additions and 23 deletions
|
|
@ -397,6 +397,78 @@ int main() {
|
|||
fs::remove_all(retryDir);
|
||||
}
|
||||
|
||||
// --- a message deleted while it is sending: imsd's late answers write nothing -------------
|
||||
{
|
||||
const fs::path deletedDir = fs::temp_directory_path() / "imsd-messages-sync-deleted";
|
||||
fs::remove_all(deletedDir);
|
||||
MessageStore deletedStore(deletedDir);
|
||||
Fake bus6;
|
||||
Recorder events6;
|
||||
Sync deleting(deletedStore, bus6, events6, "31");
|
||||
deleting.OnImsdAppeared();
|
||||
bus6.AnswerSubscribe();
|
||||
bus6.AnswerStatus(Online());
|
||||
bus6.AnswerList(std::vector<ReceivedMessage>{});
|
||||
// A delivered message of an earlier imsd run: its id is its own, and
|
||||
// no later Send gets it (imsd's ids are unique across its runs).
|
||||
Message earlier;
|
||||
earlier.conversation = "+31621308050";
|
||||
earlier.peer = "+31621308050";
|
||||
earlier.direction = Direction::Outgoing;
|
||||
earlier.text = "from an earlier imsd run";
|
||||
earlier.sortTime = earlier.received = 900;
|
||||
earlier.state = MessageState::Delivered;
|
||||
earlier.read = true;
|
||||
earlier.imsdId = "ims-sms-1700000000-20";
|
||||
const std::uint64_t older = *deletedStore.Add(earlier);
|
||||
const std::uint64_t sent = deleting.Send("+31621308050", "+31621308050", "deleted while it is sending");
|
||||
bus6.AnswerSend(std::string("ims-sms-1700000900-1"));
|
||||
deleting.Erase(sent);
|
||||
deleting.OnStateChanged("ims-sms-1700000900-1", "sent", "");
|
||||
deleting.OnStateChanged("ims-sms-1700000900-1", "delivery-failed", ""); // the report after the sent, for a deleted message
|
||||
Check(!deletedStore.Get(sent) && deletedStore.Size() == 1, "a message deleted after imsd took it stays deleted when its state comes");
|
||||
const std::uint64_t early = deleting.Send("+31621308050", "+31621308050", "deleted before imsd answered");
|
||||
deleting.Erase(early);
|
||||
bus6.AnswerSend(std::string("ims-sms-1700000900-2"));
|
||||
deleting.OnStateChanged("ims-sms-1700000900-2", "sent", "");
|
||||
const std::uint64_t failing = deleting.Send("+31621308051", "+31621308051", "in a conversation deleted before imsd answered");
|
||||
deleting.EraseConversation("+31621308051");
|
||||
bus6.AnswerSend(std::unexpected(Error(BusError::Kind::Other, "late failure")));
|
||||
Check(deletedStore.Size() == 1 && !deletedStore.Get(early) && !deletedStore.Get(failing), "a deleted message stays deleted when imsd's answer to its Send comes");
|
||||
Check(std::ranges::count_if(events6.logs, [](const std::string& l) { return l.contains("after it was deleted"); }) == 2 && std::ranges::count_if(events6.logs, [](const std::string& l) { return l.contains("not on record"); }) == 3, "each late answer is logged: the Send answers as deleted, the states as not on record");
|
||||
// A received text deleted while imsd still holds its copy (polkit
|
||||
// refused the delete) is not imported again by a later List; once
|
||||
// imsd no longer lists it, it is forgotten.
|
||||
deleting.Refresh();
|
||||
bus6.AnswerList(std::vector<ReceivedMessage>{ Text("ims-sms-in-9-1", "+31621308052", "kept by imsd", 950) });
|
||||
const std::vector<Message> conv9 = deletedStore.Conversation("+31621308052");
|
||||
Check(conv9.size() == 1, "the text is imported");
|
||||
bus6.AnswerDelete(Error(BusError::Kind::AccessDenied, "not authorized"));
|
||||
deleting.Erase(conv9[0].id);
|
||||
Check(deletedStore.ErasedImsdIds().contains("ims-sms-in-9-1"), "its imsd id is remembered when the user deletes it");
|
||||
deleting.Refresh();
|
||||
bus6.AnswerList(std::vector<ReceivedMessage>{ Text("ims-sms-in-9-1", "+31621308052", "kept by imsd", 950) });
|
||||
Check(deletedStore.Conversation("+31621308052").empty(), "listed again, it is not imported again");
|
||||
// A text deleted while a List is in flight: that List's answer is
|
||||
// older than the memory and does not forget it — it forgets the id
|
||||
// remembered before it went out; the next one forgets that one too.
|
||||
deleting.Refresh(); // ims-sms-in-9-1 is remembered as this List goes out
|
||||
deleting.OnReceived(Text("ims-sms-in-9-2", "+31621308053", "in flight", 960));
|
||||
const std::vector<Message> conv9b = deletedStore.Conversation("+31621308053");
|
||||
Check(conv9b.size() == 1, "a text signalled while a List is in flight is imported");
|
||||
bus6.AnswerDelete(Error(BusError::Kind::AccessDenied, "not authorized"));
|
||||
deleting.Erase(conv9b[0].id);
|
||||
bus6.AnswerList(std::vector<ReceivedMessage>{});
|
||||
Check(!deletedStore.ErasedImsdIds().contains("ims-sms-in-9-1") && deletedStore.ErasedImsdIds().contains("ims-sms-in-9-2"), "the List sent before the delete forgets the id remembered before it went out, not the one remembered while it was in flight");
|
||||
bus6.AnswerList(std::vector<ReceivedMessage>{});
|
||||
Check(!deletedStore.ErasedImsdIds().contains("ims-sms-in-9-2"), "the next List, sent after it, forgets that one too");
|
||||
// Another client's text (imsd tells every subscriber every state):
|
||||
// its id is on no record here, and the older message keeps its state.
|
||||
deleting.OnStateChanged("ims-sms-1700000900-3", "failed", "RP-ERROR cause 38");
|
||||
Check(deletedStore.Get(older) && deletedStore.Get(older)->state == MessageState::Delivered && events6.logs.back().contains("not on record"), "a state for another client's text is logged and leaves every message alone");
|
||||
fs::remove_all(deletedDir);
|
||||
}
|
||||
|
||||
// --- a restart: a pending send of an earlier run is failed at start; a late word applies ---
|
||||
Message stale;
|
||||
stale.conversation = "+31621308050";
|
||||
|
|
|
|||
Loading…
Reference in a new issue