|
| 1 | +using System.Threading.Tasks; |
| 2 | +using LaunchDarkly.Logging; |
| 3 | +using LaunchDarkly.Sdk.Server.Interfaces; |
| 4 | +using Xunit; |
| 5 | +using Xunit.Abstractions; |
| 6 | + |
| 7 | +using static LaunchDarkly.Sdk.Server.Interfaces.BigSegmentStoreTypes; |
| 8 | + |
| 9 | +namespace LaunchDarkly.Sdk.Server.SharedTests.BigSegmentStore |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A configurable Xunit test class for all implementations of <c>IBigSegmentStore</c>. |
| 13 | + /// </summary> |
| 14 | + /// <remarks> |
| 15 | + /// <para> |
| 16 | + /// Each implementation of those interfaces should define a test class that is a subclass of this |
| 17 | + /// class for their implementation type, and run it in the unit tests for their project. |
| 18 | + /// </para> |
| 19 | + /// <para> |
| 20 | + /// You must override the <see cref="Configuration"/> property to provide details specific to |
| 21 | + /// your implementation type. |
| 22 | + /// </para> |
| 23 | + /// </remarks> |
| 24 | + public abstract class BigSegmentStoreBaseTests |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Override this method to create the configuration for the test suite. |
| 28 | + /// </summary> |
| 29 | + protected abstract BigSegmentStoreTestConfig Configuration { get; } |
| 30 | + |
| 31 | + private const string prefix = "testprefix"; |
| 32 | + private const string fakeUserHash = "userhash"; |
| 33 | + private const string segmentRef1 = "key1", segmentRef2 = "key2", segmentRef3 = "key3"; |
| 34 | + private static readonly string[] allSegmentRefs = new string[] { segmentRef1, segmentRef2, segmentRef3 }; |
| 35 | + |
| 36 | + private readonly ILogAdapter _testLogging; |
| 37 | + |
| 38 | + protected BigSegmentStoreBaseTests() |
| 39 | + { |
| 40 | + _testLogging = Logs.None; |
| 41 | + } |
| 42 | + |
| 43 | + protected BigSegmentStoreBaseTests(ITestOutputHelper testOutput) |
| 44 | + { |
| 45 | + _testLogging = TestLogging.TestOutputAdapter(testOutput); |
| 46 | + } |
| 47 | + |
| 48 | + private IBigSegmentStore MakeStore() |
| 49 | + { |
| 50 | + var context = new LdClientContext(new BasicConfiguration("sdk-key", false, _testLogging.Logger("")), |
| 51 | + LaunchDarkly.Sdk.Server.Configuration.Default("sdk-key")); |
| 52 | + return Configuration.StoreFactoryFunc(prefix).CreateBigSegmentStore(context); |
| 53 | + } |
| 54 | + |
| 55 | + private async Task<IBigSegmentStore> MakeEmptyStore() |
| 56 | + { |
| 57 | + var store = MakeStore(); |
| 58 | + try |
| 59 | + { |
| 60 | + await Configuration.ClearDataAction(prefix); |
| 61 | + } |
| 62 | + catch |
| 63 | + { |
| 64 | + store.Dispose(); |
| 65 | + throw; |
| 66 | + } |
| 67 | + return store; |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async void MissingMetadata() |
| 72 | + { |
| 73 | + using (var store = await MakeEmptyStore()) |
| 74 | + { |
| 75 | + Assert.Null(await store.GetMetadataAsync()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public async void ValidMetadata() |
| 81 | + { |
| 82 | + using (var store = await MakeEmptyStore()) |
| 83 | + { |
| 84 | + var metadata = new StoreMetadata { LastUpToDate = UnixMillisecondTime.Now }; |
| 85 | + await Configuration.SetMetadataAction(prefix, metadata); |
| 86 | + |
| 87 | + var result = await store.GetMetadataAsync(); |
| 88 | + Assert.NotNull(result); |
| 89 | + Assert.Equal(metadata.LastUpToDate, result.Value.LastUpToDate); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public async void MembershipNotFound() |
| 95 | + { |
| 96 | + using (var store = await MakeEmptyStore()) |
| 97 | + { |
| 98 | + var membership = await store.GetMembershipAsync(fakeUserHash); |
| 99 | + |
| 100 | + // Either null or an empty membership is allowed in this case |
| 101 | + if (membership != null) |
| 102 | + { |
| 103 | + AssertEqualMembership(NewMembershipFromSegmentRefs(null, null), membership); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [Theory] |
| 109 | + [InlineData(new string[] { segmentRef1 }, new string[] { })] |
| 110 | + [InlineData(new string[] { segmentRef1, segmentRef2 }, new string[] { })] |
| 111 | + [InlineData(new string[] { }, new string[] { segmentRef1 })] |
| 112 | + [InlineData(new string[] { }, new string[] { segmentRef1, segmentRef2 })] |
| 113 | + [InlineData(new string[] { segmentRef1, segmentRef2 }, new string[] { segmentRef2, segmentRef3 })] |
| 114 | + public async void MembershipFound(string[] includes, string[] excludes) |
| 115 | + { |
| 116 | + using (var store = await MakeEmptyStore()) |
| 117 | + { |
| 118 | + await Configuration.SetSegmentsAction(prefix, fakeUserHash, includes, excludes); |
| 119 | + |
| 120 | + var membership = await store.GetMembershipAsync(fakeUserHash); |
| 121 | + |
| 122 | + AssertEqualMembership(NewMembershipFromSegmentRefs(includes, excludes), membership); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static void AssertEqualMembership(IMembership expected, IMembership actual) |
| 127 | + { |
| 128 | + if (actual.GetType().FullName.StartsWith("LaunchDarkly.Sdk.Server.Internal.BigSegments.MembershipBuilder")) |
| 129 | + { |
| 130 | + // The store implementation is using our standard membership types, so we can rely on the |
| 131 | + // standard equality test for those |
| 132 | + Assert.Equal(expected, actual); |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + // The store implementation has implemented IMembership in some other way, so we have to |
| 137 | + // check for the inclusion or exclusion of specific keys |
| 138 | + foreach (var segmentRef in allSegmentRefs) |
| 139 | + { |
| 140 | + if (actual.CheckMembership(segmentRef) != expected.CheckMembership(segmentRef)) |
| 141 | + { |
| 142 | + Assert.True(false, string.Format("expected membership for {0} to be {1} but was {2}", |
| 143 | + segmentRef, expected.CheckMembership(segmentRef), actual.CheckMembership(segmentRef))); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments