|
| 1 | +defmodule Sequin.ConsumersRuntime.AtLeastOnceVerificationTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Sequin.ConsumersRuntime.AtLeastOnceVerification |
| 5 | + alias Sequin.Factory |
| 6 | + alias Sequin.Redis |
| 7 | + |
| 8 | + setup do |
| 9 | + consumer_id = Factory.uuid() |
| 10 | + commit_key = "consumer:#{consumer_id}:commit_verification" |
| 11 | + |
| 12 | + on_exit(fn -> |
| 13 | + Redis.command(["DEL", commit_key]) |
| 14 | + end) |
| 15 | + |
| 16 | + %{consumer_id: consumer_id} |
| 17 | + end |
| 18 | + |
| 19 | + describe "record_commit/3" do |
| 20 | + test "records a commit tuple with timestamp", %{consumer_id: consumer_id} do |
| 21 | + commit = {123, 456} |
| 22 | + timestamp = System.system_time(:second) |
| 23 | + |
| 24 | + assert :ok = AtLeastOnceVerification.record_commit(consumer_id, commit, timestamp) |
| 25 | + |
| 26 | + # Verify the commit was recorded with correct score |
| 27 | + {:ok, commits} = AtLeastOnceVerification.all_commits(consumer_id) |
| 28 | + assert length(commits) == 1 |
| 29 | + assert List.first(commits) == {commit, timestamp} |
| 30 | + end |
| 31 | + |
| 32 | + test "can record multiple commits", %{consumer_id: consumer_id} do |
| 33 | + commits = [ |
| 34 | + {{100, 200}, 1000}, |
| 35 | + {{101, 201}, 1001}, |
| 36 | + {{102, 202}, 1002} |
| 37 | + ] |
| 38 | + |
| 39 | + Enum.each(commits, fn {commit, ts} -> |
| 40 | + assert :ok = AtLeastOnceVerification.record_commit(consumer_id, commit, ts) |
| 41 | + end) |
| 42 | + |
| 43 | + # Verify all commits were recorded |
| 44 | + {:ok, count} = AtLeastOnceVerification.count_commits(consumer_id) |
| 45 | + assert String.to_integer(count) == 3 |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe "remove_commit/2" do |
| 50 | + test "removes a specific commit tuple", %{consumer_id: consumer_id} do |
| 51 | + commit = {123, 456} |
| 52 | + timestamp = System.system_time(:second) |
| 53 | + |
| 54 | + :ok = AtLeastOnceVerification.record_commit(consumer_id, commit, timestamp) |
| 55 | + assert :ok = AtLeastOnceVerification.remove_commit(consumer_id, commit) |
| 56 | + |
| 57 | + # Verify the commit was removed |
| 58 | + {:ok, members} = Redis.command(["ZRANGE", "consumer:#{consumer_id}:commit_verification", 0, -1]) |
| 59 | + assert members == [] |
| 60 | + end |
| 61 | + |
| 62 | + test "returns ok when removing non-existent commit", %{consumer_id: consumer_id} do |
| 63 | + assert :ok = AtLeastOnceVerification.remove_commit(consumer_id, {999, 999}) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe "get_unverified_commits/2" do |
| 68 | + test "returns commits older than specified timestamp", %{consumer_id: consumer_id} do |
| 69 | + now = System.system_time(:second) |
| 70 | + |
| 71 | + old_commits = [ |
| 72 | + {{100, 200}, now - 100}, |
| 73 | + {{101, 201}, now - 50} |
| 74 | + ] |
| 75 | + |
| 76 | + new_commit = {{102, 202}, now} |
| 77 | + |
| 78 | + # Record all commits |
| 79 | + Enum.each(old_commits, fn {commit, ts} -> |
| 80 | + :ok = AtLeastOnceVerification.record_commit(consumer_id, commit, ts) |
| 81 | + end) |
| 82 | + |
| 83 | + :ok = AtLeastOnceVerification.record_commit(consumer_id, elem(new_commit, 0), elem(new_commit, 1)) |
| 84 | + |
| 85 | + # Get commits older than (now - 25) |
| 86 | + {:ok, unverified} = AtLeastOnceVerification.get_unverified_commits(consumer_id, now - 25) |
| 87 | + |
| 88 | + assert length(unverified) == 2 |
| 89 | + assert Enum.all?(unverified, fn {_commit, ts} -> ts < now - 25 end) |
| 90 | + end |
| 91 | + |
| 92 | + test "returns empty list when no commits exist", %{consumer_id: consumer_id} do |
| 93 | + {:ok, unverified} = AtLeastOnceVerification.get_unverified_commits(consumer_id, System.system_time(:second)) |
| 94 | + assert unverified == [] |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe "trim_commits/2" do |
| 99 | + test "removes commits older than specified timestamp", %{consumer_id: consumer_id} do |
| 100 | + now = System.system_time(:second) |
| 101 | + |
| 102 | + commits = [ |
| 103 | + # old |
| 104 | + {{100, 200}, now - 100}, |
| 105 | + # old |
| 106 | + {{101, 201}, now - 50}, |
| 107 | + # current |
| 108 | + {{102, 202}, now} |
| 109 | + ] |
| 110 | + |
| 111 | + # Record all commits |
| 112 | + Enum.each(commits, fn {commit, ts} -> |
| 113 | + :ok = AtLeastOnceVerification.record_commit(consumer_id, commit, ts) |
| 114 | + end) |
| 115 | + |
| 116 | + # Trim commits older than (now - 25) |
| 117 | + assert :ok = AtLeastOnceVerification.trim_commits(consumer_id, now - 25) |
| 118 | + |
| 119 | + # Verify only newer commit remains |
| 120 | + {:ok, remaining} = AtLeastOnceVerification.count_commits(consumer_id) |
| 121 | + assert String.to_integer(remaining) == 1 |
| 122 | + |
| 123 | + {:ok, [member]} = AtLeastOnceVerification.all_commits(consumer_id) |
| 124 | + assert member == {{102, 202}, now} |
| 125 | + end |
| 126 | + |
| 127 | + test "handles empty set gracefully", %{consumer_id: consumer_id} do |
| 128 | + assert :ok = AtLeastOnceVerification.trim_commits(consumer_id, System.system_time(:second)) |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments