private bool TryDeserializeStateVer1(string serialized, out ABTestEnrollment enrollment) { enrollment = null; try { var v1 = JsonConvert.DeserializeObject <StateVersion1>(serialized); if (v1 == null || v1.SchemaVersion != SchemaVersion1 || IsNotPercentage(v1.PreviewSearchBucket)) { return(false); } enrollment = new ABTestEnrollment( ABTestEnrollmentState.Upgraded, SchemaVersion2, v1.PreviewSearchBucket, packageDependentBucket: GetRandomWholePercentage()); _telemetryService.TrackABTestEnrollmentUpgraded( enrollment.SchemaVersion, enrollment.PreviewSearchBucket, enrollment.PackageDependentBucket); return(true); } catch (JsonException) { return(false); } }
private bool TryDeserializeStateVer2(string serialized, out ABTestEnrollment enrollment) { enrollment = null; try { var v2 = JsonConvert.DeserializeObject <StateVersion2>(serialized); if (v2 == null || v2.SchemaVersion != SchemaVersion2 || IsNotPercentage(v2.PreviewSearchBucket) || IsNotPercentage(v2.PackageDependentBucket)) { return(false); } enrollment = new ABTestEnrollment( ABTestEnrollmentState.Active, v2.SchemaVersion, v2.PreviewSearchBucket, v2.PackageDependentBucket); return(true); } catch (JsonException) { return(false); } }
public bool TryDeserialize(string serialized, out ABTestEnrollment enrollment) { enrollment = null; if (string.IsNullOrWhiteSpace(serialized)) { return(false); } try { var v1 = JsonConvert.DeserializeObject <StateVersion1>(serialized); if (v1 == null || v1.SchemaVersion != SchemaVersion1 || IsNotPercentage(v1.PreviewSearchBucket)) { return(false); } enrollment = new ABTestEnrollment( ABTestEnrollmentState.Active, v1.SchemaVersion, v1.PreviewSearchBucket); return(true); } catch (JsonException) { return(false); } }
private bool TryDeserializeStateVer2(string serialized, out ABTestEnrollment enrollment) { enrollment = null; try { var v2 = JsonConvert.DeserializeObject <StateVersion2OrVersion3>(serialized); if (v2 == null || v2.SchemaVersion != SchemaVersion2 || IsNotPercentage(v2.PreviewSearchBucket) || IsNotPercentage(v2.PackageDependentBucket)) { return(false); } enrollment = new ABTestEnrollment( ABTestEnrollmentState.Upgraded, SchemaVersion3, v2.PreviewSearchBucket, v2.PackageDependentBucket); _telemetryService.TrackABTestEnrollmentUpgraded( SchemaVersion2, enrollment.SchemaVersion, enrollment.PreviewSearchBucket, enrollment.PackageDependentBucket); return(true); } catch (JsonException) { return(false); } }
public bool TryDeserialize(string serialized, out ABTestEnrollment enrollment) { enrollment = null; if (string.IsNullOrWhiteSpace(serialized)) { return(false); } return(TryDeserializeStateVer2(serialized, out enrollment) || TryDeserializeStateVer1(serialized, out enrollment)); }
public Facts() { HttpContext = new Mock <HttpContextBase>(); FeatureFlagService = new Mock <IFeatureFlagService>(); EnrollmentFactory = new Mock <IABTestEnrollmentFactory>(); ContentObjectService = new Mock <IContentObjectService>(); Configuration = new Mock <IABTestConfiguration>(); TelemetryService = new Mock <ITelemetryService>(); Logger = new Mock <ILogger <CookieBasedABTestService> >(); DateTimeProvider = new Mock <IDateTimeProvider>(); User = new User(); InitializedEnrollment = new ABTestEnrollment( ABTestEnrollmentState.FirstHit, schemaVersion: 1, previewSearchBucket: 23, packageDependentBucket: 47); DeserializedEnrollment = new ABTestEnrollment( ABTestEnrollmentState.Active, schemaVersion: 1, previewSearchBucket: 42, packageDependentBucket: 58); OutEnrollment = DeserializedEnrollment; SerializedEnrollment = "fake-serialization"; RequestCookies = new HttpCookieCollection(); ResponseCookies = new HttpCookieCollection(); PreviewSearchPercentage = 100; HttpContext.Setup(x => x.Request.Cookies).Returns(() => RequestCookies); HttpContext.Setup(x => x.Response.Cookies).Returns(() => ResponseCookies); FeatureFlagService.SetReturnsDefault(true); EnrollmentFactory.Setup(x => x.Initialize()).Returns(() => InitializedEnrollment); EnrollmentFactory .Setup(x => x.TryDeserialize(It.IsAny <string>(), out OutEnrollment)) .Returns(true); EnrollmentFactory .Setup(x => x.Serialize(It.IsAny <ABTestEnrollment>())) .Returns(() => SerializedEnrollment); ContentObjectService.Setup(x => x.ABTestConfiguration).Returns(() => Configuration.Object); Configuration.Setup(x => x.PreviewSearchPercentage).Returns(() => PreviewSearchPercentage); CurrentTime = DateTime.UtcNow; DateTimeProvider.Setup(x => x.UtcNow).Returns(CurrentTime); Target = new CookieBasedABTestService( HttpContext.Object, FeatureFlagService.Object, EnrollmentFactory.Object, ContentObjectService.Object, TelemetryService.Object, Logger.Object, DateTimeProvider.Object); }
public void ProducesExpectedString() { var enrollment = new ABTestEnrollment( ABTestEnrollmentState.Active, schemaVersion: 1, previewSearchBucket: 42); var serialized = Target.Serialize(enrollment); Assert.Equal(@"{""v"":1,""ps"":42}", serialized); }
public void ProducesExpectedStringVersionTwo() { var enrollment = new ABTestEnrollment( ABTestEnrollmentState.Active, schemaVersion: 2, previewSearchBucket: 42, packageDependentBucket: 74); var serialized = Target.Serialize(enrollment); Assert.Equal(@"{""v"":2,""ps"":42,""pd"":74}", serialized); }
public void ComparesEnrollmentToConfig(int enrollment, int config, bool enabled) { InitializedEnrollment = new ABTestEnrollment( ABTestEnrollmentState.FirstHit, schemaVersion: 1, previewSearchBucket: enrollment); Configuration.Setup(x => x.PreviewSearchPercentage).Returns(config); var result = Target.IsPreviewSearchEnabled(User); Assert.Equal(enabled, result); }
public void ProducesExpectedString(int schemaVersion, ABTestEnrollmentState state) { var enrollment = new ABTestEnrollment( state: state, schemaVersion: schemaVersion, previewSearchBucket: 42, packageDependentBucket: 82); var serialized = Target.Serialize(enrollment); Assert.Equal(@"{""v"":3,""ps"":42,""pd"":82}", serialized); }
public void SerializeWithNotImplementedSchemaVersion_ThrowNotImplementedException(int schemaVersion) { var enrollment = new ABTestEnrollment( state: It.IsAny <ABTestEnrollmentState>(), schemaVersion: schemaVersion, previewSearchBucket: It.IsAny <int>(), packageDependentBucket: It.IsAny <int>()); var exception = Assert.Throws <NotImplementedException>(() => Target.Serialize(enrollment)); Assert.Equal($"Serializing schema version {schemaVersion} is not implemented.", exception.Message); }
public ABTestEnrollment Initialize() { var enrollment = new ABTestEnrollment( ABTestEnrollmentState.FirstHit, SchemaVersion1, previewSearchBucket: GetRandomWholePercentage()); _telemetryService.TrackABTestEnrollmentInitialized( enrollment.SchemaVersion, enrollment.PreviewSearchBucket); return(enrollment); }
public string Serialize(ABTestEnrollment enrollment) { if (enrollment.SchemaVersion != SchemaVersion1) { throw new NotImplementedException($"Serializing schema version {enrollment.SchemaVersion} is not implemented."); } var deserialized = new StateVersion1 { SchemaVersion = SchemaVersion1, PreviewSearchBucket = enrollment.PreviewSearchBucket, }; return(JsonConvert.SerializeObject(deserialized)); }