|
25 | 25 | using ObjCRuntime; |
26 | 26 | #endif |
27 | 27 |
|
28 | | -#if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT |
| 28 | +#if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT || ENABLE_RUNTIME_DRIFT_CASE_DATABASE_QUERY_GETDATA |
29 | 29 | using Firebase.Database; |
| 30 | +using FirebaseCoreOptions = Firebase.Core.Options; |
30 | 31 | using Foundation; |
31 | 32 | using ObjCRuntime; |
32 | 33 | #endif |
@@ -736,6 +737,129 @@ void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEven |
736 | 737 | } |
737 | 738 | #endif |
738 | 739 |
|
| 740 | +#if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_QUERY_GETDATA |
| 741 | + static async Task<string> VerifyDatabaseQueryGetDataAsync() |
| 742 | + { |
| 743 | + const string selector = "getDataWithCompletionBlock:"; |
| 744 | + |
| 745 | + var signature = typeof(DatabaseQuery).GetMethod( |
| 746 | + nameof(DatabaseQuery.GetData), |
| 747 | + BindingFlags.Instance | BindingFlags.Public, |
| 748 | + binder: null, |
| 749 | + types: new[] { typeof(DataSnapshotCompletionHandler) }, |
| 750 | + modifiers: null); |
| 751 | + if (signature?.ReturnType != typeof(void)) |
| 752 | + { |
| 753 | + throw new InvalidOperationException( |
| 754 | + $"Expected managed API '{typeof(DatabaseQuery).FullName}.{nameof(DatabaseQuery.GetData)}({typeof(DataSnapshotCompletionHandler).FullName})' " + |
| 755 | + $"to return void for selector '{selector}', observed '{signature?.ReturnType.FullName ?? "<missing>"}'."); |
| 756 | + } |
| 757 | + |
| 758 | + var projectId = FirebaseCoreOptions.DefaultInstance?.ProjectId |
| 759 | + ?? throw new InvalidOperationException("Firebase.Core.Options.ProjectId returned null before Database query validation."); |
| 760 | + var database = Firebase.Database.Database.From($"https://{projectId}-default-rtdb.firebaseio.com"); |
| 761 | + var root = database.GetRootReference(); |
| 762 | + var query = root.GetQueryOrderedByKey(); |
| 763 | + if (query is null) |
| 764 | + { |
| 765 | + throw new InvalidOperationException("Firebase.Database.DatabaseReference.GetQueryOrderedByKey returned null."); |
| 766 | + } |
| 767 | + |
| 768 | + if (!query.RespondsToSelector(new Selector(selector))) |
| 769 | + { |
| 770 | + throw new InvalidOperationException($"Native FIRDatabaseQuery does not respond to expected selector '{selector}'."); |
| 771 | + } |
| 772 | + |
| 773 | + var completionSource = new TaskCompletionSource<(NSError? Error, DataSnapshot? Snapshot)>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 774 | + var completionInvoked = false; |
| 775 | + NSError? callbackError = null; |
| 776 | + DataSnapshot? callbackSnapshot = null; |
| 777 | + NSException? marshaledException = null; |
| 778 | + MarshalObjectiveCExceptionMode? marshaledExceptionMode = null; |
| 779 | + |
| 780 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 781 | + { |
| 782 | + marshaledException ??= args.Exception; |
| 783 | + marshaledExceptionMode ??= args.ExceptionMode; |
| 784 | + } |
| 785 | + |
| 786 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 787 | + try |
| 788 | + { |
| 789 | + try |
| 790 | + { |
| 791 | + query.GetData((error, snapshot) => |
| 792 | + { |
| 793 | + completionInvoked = true; |
| 794 | + callbackError = error; |
| 795 | + callbackSnapshot = snapshot; |
| 796 | + completionSource.TrySetResult((error, snapshot)); |
| 797 | + }); |
| 798 | + } |
| 799 | + catch (ObjCException ex) |
| 800 | + { |
| 801 | + throw new InvalidOperationException( |
| 802 | + $"Selector '{selector}' should not throw after the missing DatabaseQuery binding is added, but observed {ex.GetType().FullName}. " + |
| 803 | + $"Managed query runtime type: {query.GetType().FullName}. " + |
| 804 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 805 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 806 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 807 | + ex); |
| 808 | + } |
| 809 | + |
| 810 | + if (marshaledException is not null) |
| 811 | + { |
| 812 | + throw new InvalidOperationException( |
| 813 | + $"Selector '{selector}' completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 814 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 815 | + } |
| 816 | + |
| 817 | + var completedTask = await Task.WhenAny(completionSource.Task, Task.Delay(AsyncTimeout)); |
| 818 | + if (completedTask != completionSource.Task) |
| 819 | + { |
| 820 | + throw new TimeoutException( |
| 821 | + $"Selector '{selector}' did not invoke its completion callback within {AsyncTimeout.TotalSeconds} seconds after crossing the native DatabaseQuery boundary."); |
| 822 | + } |
| 823 | + |
| 824 | + string callbackDetail; |
| 825 | + var (completedError, completedSnapshot) = await completionSource.Task; |
| 826 | + if (!completionInvoked) |
| 827 | + { |
| 828 | + throw new InvalidOperationException( |
| 829 | + $"Selector '{selector}' completed without throwing, but the completion callback was never marked as invoked."); |
| 830 | + } |
| 831 | + |
| 832 | + if (!ReferenceEquals(callbackError, completedError) || !ReferenceEquals(callbackSnapshot, completedSnapshot)) |
| 833 | + { |
| 834 | + throw new InvalidOperationException("Database query getData callback state did not match the completed task payload."); |
| 835 | + } |
| 836 | + |
| 837 | + callbackDetail = completedError is not null |
| 838 | + ? $"completion callback returned Firebase error {FormatNSError(completedError)}" |
| 839 | + : completedSnapshot is not null |
| 840 | + ? $"completion callback returned snapshot type {completedSnapshot.GetType().FullName} with key '{FormatDetail(completedSnapshot.Key)}'" |
| 841 | + : "completion callback returned neither snapshot nor Firebase error"; |
| 842 | + |
| 843 | + if (marshaledException is not null) |
| 844 | + { |
| 845 | + throw new InvalidOperationException( |
| 846 | + $"Selector '{selector}' completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 847 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 848 | + } |
| 849 | + |
| 850 | + return |
| 851 | + $"Selector '{selector}' crossed the native DatabaseQuery boundary. " + |
| 852 | + $"Managed query runtime type: {query.GetType().FullName}. " + |
| 853 | + $"Query reference URL: {query.Reference.Url}. " + |
| 854 | + $"Callback detail: {callbackDetail}."; |
| 855 | + } |
| 856 | + finally |
| 857 | + { |
| 858 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 859 | + } |
| 860 | + } |
| 861 | +#endif |
| 862 | + |
739 | 863 | #if ENABLE_RUNTIME_DRIFT_CASE_ABTESTING_UPDATEEXPERIMENTS |
740 | 864 | static async Task<string> VerifyABTestingUpdateExperimentsAsync() |
741 | 865 | { |
|
0 commit comments