[Network] Fix NWTxtRecord.Apply. Fixes xamarin/maccore#2036. (#7281)

The block/delegate passed to NWTxtRecord.Apply is supposed to return a bool.
Not returning anything ends up with random behavior, which causes a test
failure on Catalina:

   3) TestApply (MonoTouchFixtures.Network.NWTxtRecordTest.TestApply)
        keycount
     Expected: 4
     But was:  1

     at MonoTouchFixtures.Network.NWTxtRecordTest.TestApply () [0x000a3] in /Users/builder/jenkins/workspace/xamarin-macios-pr-builder/tests/monotouch-test/Network/NWTxtRecordTest.cs:134
     at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
     at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/xamarin-macios-pr-builder/external/mono/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395

I've fixed the existing API to return 'true' always in the block callback, to
get consistent behavior, and in addition I've added a new overload that takes
a delegate that returns a bool, which allows the consumer of the API to decide
what to return.

Fixes https://github.com/xamarin/maccore/issues/2036.
This commit is contained in:
Rolf Bjarne Kvinge 2019-10-23 23:25:27 +02:00 коммит произвёл GitHub
Родитель bad61b8f5e
Коммит 4d8ec5d531
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 54 добавлений и 6 удалений

Просмотреть файл

@ -118,21 +118,45 @@ namespace Network {
[DllImport (Constants.NetworkLibrary)]
unsafe static extern bool nw_txt_record_apply (OS_nw_txt_record txt_record, ref BlockLiteral applier);
unsafe delegate void nw_txt_record_apply_t (IntPtr block, string key, NWTxtRecordFindKey found, IntPtr value, nuint valueLen);
delegate bool nw_txt_record_apply_t (IntPtr block, string key, NWTxtRecordFindKey found, IntPtr value, nuint valueLen);
unsafe static nw_txt_record_apply_t static_ApplyHandler = TrampolineApplyHandler;
#if XAMCORE_4_0
public delegate bool NWTxtRecordApplyDelegate (string key, NWTxtRecordFindKey result, ReadOnlySpan<byte> value);
#else
public delegate void NWTxtRecordApplyDelegate (string key, NWTxtRecordFindKey rersult, ReadOnlySpan<byte> value);
public delegate bool NWTxtRecordApplyDelegate2 (string key, NWTxtRecordFindKey result, ReadOnlySpan<byte> value);
#endif
[MonoPInvokeCallback (typeof (nw_txt_record_apply_t))]
unsafe static void TrampolineApplyHandler (IntPtr block, string key, NWTxtRecordFindKey found, IntPtr value, nuint valueLen)
unsafe static bool TrampolineApplyHandler (IntPtr block, string key, NWTxtRecordFindKey found, IntPtr value, nuint valueLen)
{
#if XAMCORE_4_0
var del = BlockLiteral.GetTarget<NWTxtRecordApplyDelegate> (block);
if (del != null) {
var mValue = new ReadOnlySpan<byte>((void*)value, (int)valueLen);
del (key, found, mValue);
#else
var del = BlockLiteral.GetTarget<MulticastDelegate> (block);
#endif
if (del == null)
return false;
var mValue = new ReadOnlySpan<byte> ((void*)value, (int)valueLen);
#if XAMCORE_4_0
return del (key, found, mValue);
#else
if (del is NWTxtRecordApplyDelegate apply) {
apply (key, found, mValue);
return true;
}
if (del is NWTxtRecordApplyDelegate2 apply2)
return apply2 (key, found, mValue);
return false;
#endif
}
#if !XAMCORE_4_0
[Obsolete ("Use the overload that takes an NWTxtRecordApplyDelegate2 instead.")]
#endif
[BindingImpl (BindingImplOptions.Optimizable)]
public bool Apply (NWTxtRecordApplyDelegate handler)
{
@ -148,6 +172,23 @@ namespace Network {
}
}
#if !XAMCORE_4_0
[BindingImpl (BindingImplOptions.Optimizable)]
public bool Apply (NWTxtRecordApplyDelegate2 handler)
{
if (handler == null)
throw new ArgumentNullException (nameof (handler));
BlockLiteral block_handler = new BlockLiteral ();
block_handler.SetupBlockUnsafe (static_ApplyHandler, handler);
try {
return nw_txt_record_apply (GetCheckedHandle (), ref block_handler);
} finally {
block_handler.CleanupBlock ();
}
}
#endif
[DllImport (Constants.NetworkLibrary)]
static extern unsafe bool nw_txt_record_access_key (OS_nw_txt_record txt_record, string key, ref BlockLiteral access_value);

Просмотреть файл

@ -131,7 +131,14 @@ namespace MonoTouchFixtures.Network
keyCount++;
Assert.IsTrue (keys.Contains (k), k);
});
var keyCount2 = 0;
record.Apply ((k, r, v) => {
keyCount2++;
Assert.IsTrue (keys.Contains (k), k);
return true;
});
Assert.AreEqual (keys.Count, keyCount, "keycount");
Assert.AreEqual (keys.Count, keyCount2, "keycount2");
}
[Test]