|
1 | 1 | using System.Reflection; |
2 | 2 |
|
| 3 | +#if ENABLE_RUNTIME_DRIFT_CASE_ABTESTING_UPDATEEXPERIMENTS |
| 4 | +using Firebase.ABTesting; |
| 5 | +using Foundation; |
| 6 | +using ObjCRuntime; |
| 7 | +#endif |
| 8 | + |
3 | 9 | #if ENABLE_RUNTIME_DRIFT_CASE_ABTESTING_ACTIVATEEXPERIMENT |
4 | 10 | using Firebase.ABTesting; |
5 | 11 | using Foundation; |
@@ -72,6 +78,123 @@ public static async Task<string> ExecuteConfiguredCaseAsync() |
72 | 78 | ?.Value; |
73 | 79 | } |
74 | 80 |
|
| 81 | +#if ENABLE_RUNTIME_DRIFT_CASE_ABTESTING_UPDATEEXPERIMENTS |
| 82 | + static async Task<string> VerifyABTestingUpdateExperimentsAsync() |
| 83 | + { |
| 84 | + const string selector = "updateExperimentsWithServiceOrigin:events:policy:lastStartTime:payloads:completionHandler:"; |
| 85 | + |
| 86 | + var signature = typeof(ExperimentController).GetMethod( |
| 87 | + nameof(ExperimentController.UpdateExperiments), |
| 88 | + BindingFlags.Instance | BindingFlags.Public, |
| 89 | + binder: null, |
| 90 | + types: new[] |
| 91 | + { |
| 92 | + typeof(string), |
| 93 | + typeof(LifecycleEvents), |
| 94 | + typeof(ExperimentPayloadExperimentOverflowPolicy), |
| 95 | + typeof(double), |
| 96 | + typeof(NSData[]), |
| 97 | + typeof(Action<NSError>) |
| 98 | + }, |
| 99 | + modifiers: null); |
| 100 | + if (signature is null) |
| 101 | + { |
| 102 | + throw new InvalidOperationException( |
| 103 | + $"Expected managed API '{nameof(ExperimentController.UpdateExperiments)}(string, {typeof(LifecycleEvents).FullName}, {typeof(ExperimentPayloadExperimentOverflowPolicy).FullName}, double, {typeof(NSData[]).FullName}, {typeof(Action<NSError>).FullName})' was not found."); |
| 104 | + } |
| 105 | + |
| 106 | + var parameters = signature.GetParameters(); |
| 107 | + if (parameters.Length != 6 || parameters[2].ParameterType != typeof(ExperimentPayloadExperimentOverflowPolicy)) |
| 108 | + { |
| 109 | + throw new InvalidOperationException( |
| 110 | + $"Managed signature regression: expected policy parameter type '{typeof(ExperimentPayloadExperimentOverflowPolicy).FullName}', observed '{parameters.ElementAtOrDefault(2)?.ParameterType.FullName ?? "<missing>"}'."); |
| 111 | + } |
| 112 | + |
| 113 | + var controller = ExperimentController.SharedInstance; |
| 114 | + if (controller is null) |
| 115 | + { |
| 116 | + throw new InvalidOperationException("Firebase.ABTesting.ExperimentController.SharedInstance returned null after App.Configure()."); |
| 117 | + } |
| 118 | + |
| 119 | + var events = new LifecycleEvents |
| 120 | + { |
| 121 | + SetExperimentEventName = new NSString("codex_set_experiment"), |
| 122 | + ActivateExperimentEventName = new NSString("codex_activate_experiment"), |
| 123 | + ClearExperimentEventName = new NSString("codex_clear_experiment"), |
| 124 | + TimeoutExperimentEventName = new NSString("codex_timeout_experiment"), |
| 125 | + ExpireExperimentEventName = new NSString("codex_expire_experiment"), |
| 126 | + }; |
| 127 | + var policy = ExperimentPayloadExperimentOverflowPolicy.DiscardOldest; |
| 128 | + var payloads = Array.Empty<NSData>(); |
| 129 | + var completionInvoked = false; |
| 130 | + NSError? completionError = null; |
| 131 | + NSException? marshaledException = null; |
| 132 | + MarshalObjectiveCExceptionMode? marshaledExceptionMode = null; |
| 133 | + var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 134 | + |
| 135 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 136 | + { |
| 137 | + marshaledException ??= args.Exception; |
| 138 | + marshaledExceptionMode ??= args.ExceptionMode; |
| 139 | + } |
| 140 | + |
| 141 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 142 | + try |
| 143 | + { |
| 144 | + try |
| 145 | + { |
| 146 | + controller.UpdateExperiments("codex", events, policy, -1, payloads, error => |
| 147 | + { |
| 148 | + completionInvoked = true; |
| 149 | + completionError = error; |
| 150 | + completionSource.TrySetResult(true); |
| 151 | + }); |
| 152 | + } |
| 153 | + catch (ObjCException ex) |
| 154 | + { |
| 155 | + throw new InvalidOperationException( |
| 156 | + $"Selector '{selector}' should not throw with the corrected enum binding, but observed {ex.GetType().FullName}. " + |
| 157 | + $"Managed policy argument type: {policy.GetType().FullName}. Policy value: {(int)policy}. " + |
| 158 | + $"Payload array type: {payloads.GetType().FullName}. Payload count: {payloads.Length}. " + |
| 159 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 160 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 161 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 162 | + ex); |
| 163 | + } |
| 164 | + |
| 165 | + var completedTask = await Task.WhenAny(completionSource.Task, Task.Delay(AsyncTimeout)); |
| 166 | + if (completedTask != completionSource.Task) |
| 167 | + { |
| 168 | + throw new TimeoutException( |
| 169 | + $"Selector '{selector}' did not invoke its completion callback within {AsyncTimeout.TotalSeconds} seconds."); |
| 170 | + } |
| 171 | + |
| 172 | + if (!completionInvoked) |
| 173 | + { |
| 174 | + throw new InvalidOperationException( |
| 175 | + $"Selector '{selector}' completed without throwing, but the completion callback was never marked as invoked."); |
| 176 | + } |
| 177 | + |
| 178 | + if (marshaledException is not null) |
| 179 | + { |
| 180 | + throw new InvalidOperationException( |
| 181 | + $"Selector '{selector}' completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 182 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 183 | + } |
| 184 | + |
| 185 | + return |
| 186 | + $"Selector '{selector}' completed without ObjC exception. " + |
| 187 | + $"Managed policy argument type: {parameters[2].ParameterType.FullName}. Policy value: {(int)policy}. " + |
| 188 | + $"Payload array type: {payloads.GetType().FullName}. Payload count: {payloads.Length}. " + |
| 189 | + $"CompletionInvoked: {completionInvoked}. CompletionError: {FormatDetail(completionError?.LocalizedDescription)}."; |
| 190 | + } |
| 191 | + finally |
| 192 | + { |
| 193 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 194 | + } |
| 195 | + } |
| 196 | +#endif |
| 197 | + |
75 | 198 | #if ENABLE_RUNTIME_DRIFT_CASE_ABTESTING_ACTIVATEEXPERIMENT |
76 | 199 | static Task<string> VerifyABTestingActivateExperimentAsync() |
77 | 200 | { |
|
0 commit comments