You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

48 lines
1.1 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP.Internal;
  5. using Xunit;
  6. namespace DotNetCore.CAP.Test
  7. {
  8. public class SnowflakeIdTest
  9. {
  10. [Fact]
  11. public void NextIdTest()
  12. {
  13. var instance = SnowflakeId.Default();
  14. var result = instance.NextId();
  15. var result2 = instance.NextId();
  16. Assert.True(result2 - result == 1);
  17. }
  18. [Fact]
  19. public void ConcurrentNextIdTest()
  20. {
  21. var array = new long[1000];
  22. Parallel.For(0, 1000, i =>
  23. {
  24. var id = SnowflakeId.Default().NextId();
  25. array[i] = id;
  26. });
  27. Assert.True(array.Distinct().Count() == 1000);
  28. }
  29. [Fact]
  30. public void TestNegativeWorkerId()
  31. {
  32. Assert.Throws<ArgumentException>(() => new SnowflakeId(-1));
  33. }
  34. [Fact]
  35. public void TestTooLargeWorkerId()
  36. {
  37. Assert.Throws<ArgumentException>(() => new SnowflakeId(1024));
  38. }
  39. }
  40. }