using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace InTheHand.Net.Tests{
class TestsUtils
{
public static int TimespanToMilliseconds(TimeSpan timeSpan)
{
double dd = timeSpan.TotalMilliseconds;
long ll = (long)dd;
return checked((int)ll);
}
public static void SetIsInNetcfTestRunner()
{
System.Reflection.Assembly assm = typeof(InTheHand.Net.BluetoothAddress).Assembly;
Type ttu = assm.GetType("InTheHand.Net.TestUtilities");
System.Diagnostics.Debug.Assert(ttu != null, "NO TestUtilities?!!");
if (ttu != null) {
System.Reflection.MethodInfo mi = ttu.GetMethod("SetIsInNetcfTestRunner",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
System.Diagnostics.Debug.Assert(mi != null, "NO SetIsInNetcfTestRunner?!!");
if (mi != null) {
mi.Invoke(null, null);
}
}
}
}
class TestsApmUtils
{
public static int SafeNoHangRead(Stream strm, byte[] buf, int offset, int length)
{
IAsyncResult ar = strm.BeginRead(buf, offset, length, null, null);
SafeNoHangWait(ar, "Read");
return strm.EndRead(ar);
}
public static void SafeNoHangEndWrite(Stream strm, IAsyncResult ar)
{
SafeNoHangWait(ar, "Write");
strm.EndWrite(ar);
}
public static void SafeNoHangWaitShort(IAsyncResult ar, string opName)
{
SafeNoHangWait(ar, opName, TimeSpan.FromMilliseconds(100));
}
public static void SafeNoHangWait(IAsyncResult ar, string opName)
{
SafeNoHangWait(ar, opName, new TimeSpan(0, 0, 10));
}
public static void SafeNoHangWait(IAsyncResult ar, string opName, TimeSpan timeout)
{
bool signalled = ar.AsyncWaitHandle.WaitOne(TestsUtils.TimespanToMilliseconds(timeout), false);
if (!signalled)
throw new InvalidOperationException("Test timeout while " + opName + "-ing.");
}
}
}
|