Skip to content

Commit 5d68fb8

Browse files
authored
feat(id_generator): swap params (#76)
1 parent 2ebde7a commit 5d68fb8

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/Utils/ActivityIdGenerator.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ public static class ActivityIdGenerator
1515
private static long UuidEpochDifference = 122192928000000000;
1616

1717
/// <summary>Generates an Activity ID for the given epoch timestamp and foreign ID.</summary>
18-
public static Guid GenerateId(int epoch, string foreignId)
18+
public static Guid GenerateId(string foreignId, int epoch)
1919
{
20-
return GenerateId(Epoch.AddSeconds(epoch), foreignId);
20+
return GenerateId(foreignId, Epoch.AddSeconds(epoch));
2121
}
2222

23-
/// <summary>Generates an Activity ID for the given timestamp and foreign ID.</summary>
24-
public static Guid GenerateId(DateTime timestamp, string foreignId)
23+
/// <summary>
24+
/// Generates an Activity ID for the given timestamp and foreign ID.
25+
/// <paramref name="timestamp"/> must be UTC.
26+
/// </summary>
27+
/// <exception cref="ArgumentException">Raised if the timestamp kind if not UTC.</exception>
28+
public static Guid GenerateId(string foreignId, DateTime timestamp)
2529
{
26-
var unixNano = timestamp.Ticks - EpochTicks;
30+
// The backend doesn't care about milliseconds, so we truncate the date here.
31+
var truncatedDate = new DateTime(timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour, timestamp.Minute, timestamp.Second, DateTimeKind.Utc);
32+
33+
var unixNano = truncatedDate.Ticks - EpochTicks;
2734
var t = (ulong)(UuidEpochDifference + unixNano);
2835

2936
long signedDigest;

tests/UtilsTests.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@ public class UtilsTests : TestBase
1313
public void TestIdGenerator()
1414
{
1515
// All these test cases are copied from the backend Go implementation
16-
var first = ActivityIdGenerator.GenerateId(1451260800, "123sdsd333}}}");
16+
var first = ActivityIdGenerator.GenerateId("123sdsd333}}}", 1451260800);
1717
Assert.AreEqual("f01c0000-acf5-11e5-8080-80006a8b5bc2", first.ToString());
1818

19-
var second = ActivityIdGenerator.GenerateId(1452862482, "6621934");
19+
var second = ActivityIdGenerator.GenerateId("6621934", 1452862482);
2020
Assert.AreEqual("24f9d500-bb87-11e5-8080-80012ce3cc51", second.ToString());
2121

22-
var third = ActivityIdGenerator.GenerateId(1452862489, "6621938");
22+
var third = ActivityIdGenerator.GenerateId("6621938", 1452862489);
2323
Assert.AreEqual("2925f280-bb87-11e5-8080-8001597d791f", third.ToString());
2424

25-
var fourth = ActivityIdGenerator.GenerateId(1452862492, "6621941");
25+
var fourth = ActivityIdGenerator.GenerateId("6621941", 1452862492);
2626
Assert.AreEqual("2aefb600-bb87-11e5-8080-8001226fe2f2", fourth.ToString());
2727

28-
var fifth = ActivityIdGenerator.GenerateId(1452862496, "6621945");
28+
var fifth = ActivityIdGenerator.GenerateId("6621945", 1452862496);
2929
Assert.AreEqual("2d521000-bb87-11e5-8080-800026259780", fifth.ToString());
3030

31-
var sixth = ActivityIdGenerator.GenerateId(1463914800, "top_issues_summary_557dc1d9e46fea0a4c000002");
31+
var sixth = ActivityIdGenerator.GenerateId("top_issues_summary_557dc1d9e46fea0a4c000002", 1463914800);
3232
Assert.AreEqual("53dbf800-200c-11e6-8080-800023ec2877", sixth.ToString());
3333

34-
var seventh = ActivityIdGenerator.GenerateId(1452866055, "6625387");
34+
var seventh = ActivityIdGenerator.GenerateId("6625387", 1452866055);
3535
Assert.AreEqual("76a65d80-bb8f-11e5-8080-8000530672db", seventh.ToString());
3636

37-
var eight = ActivityIdGenerator.GenerateId(1480174117, "UserPrediction:11127278");
37+
var eight = ActivityIdGenerator.GenerateId("UserPrediction:11127278", 1480174117);
3838
Assert.AreEqual("00000080-b3ed-11e6-8080-8000444ef374", eight.ToString());
3939

40-
var nineth = ActivityIdGenerator.GenerateId(1481475921, "467791-42-follow");
40+
var nineth = ActivityIdGenerator.GenerateId("467791-42-follow", 1481475921);
4141
Assert.AreEqual("ffa65e80-bfc3-11e6-8080-800027086507", nineth.ToString());
4242
}
4343

4444
[Test]
4545
public async Task TestActivityIdSameAsBackend()
4646
{
47+
var time = DateTime.UtcNow;
4748
var foreignId = Guid.NewGuid().ToString();
48-
var inputAct = new Activity("1", "test", "1") { Time = DateTime.UtcNow, ForeignId = foreignId };
49+
var inputAct = new Activity("1", "test", "1") { Time = time, ForeignId = foreignId };
4950
var activity = await this.UserFeed.AddActivityAsync(inputAct);
5051

51-
Assert.AreEqual(ActivityIdGenerator.GenerateId(activity.Time.Value, foreignId).ToString(), activity.Id);
52+
Assert.AreEqual(ActivityIdGenerator.GenerateId(activity.ForeignId, time).ToString(), activity.Id);
5253
}
5354
}
5455
}

0 commit comments

Comments
 (0)