private static void DoTest(IFormatter formatter, ISurrogateSelector surrogate, object obj, bool throwError, bool forWriting, bool forReading) { Console.Write($"{obj} by {formatter.GetType().Name}: "); formatter.SurrogateSelector = forWriting ? surrogate : null; TestExecutionContext.IsolatedContext context = throwError ? null : new TestExecutionContext.IsolatedContext(); try { using (var ms = new MemoryStream()) { try { formatter.Serialize(ms, obj); } catch (Exception e) { Console.WriteLine($"Serialization failed: {e}"); if (throwError) throw; return; } Console.WriteLine($"{ms.Length} bytes."); if (dumpSerContent) #pragma warning disable 162 Console.WriteLine(ms.ToArray().ToRawString()); #pragma warning restore 162 formatter.SurrogateSelector = forReading ? surrogate : null; ms.Position = 0L; try { object result = formatter.Deserialize(ms); AssertDeepEquals(obj, result); } catch (Exception e) { Console.WriteLine($"Deserialization failed: {e}"); if (throwError) throw; } } } finally { context?.Dispose(); } }
protected static void RunMultipleTimes(Action action, int retryCount, TimeSpan standOff) { var isolatedContext = new TestExecutionContext.IsolatedContext(); try { Policy.Handle <AssertionException>().WaitAndRetry(retryCount, i => standOff, (ex, ts, rc, c) => { isolatedContext.Dispose(); isolatedContext = new TestExecutionContext.IsolatedContext(); Console.WriteLine($" Assert failed: {ex.Message}\r\n Retrying assert after delay of {standOff.TotalSeconds}s...\r\n"); Task.Delay(standOff).GetAwaiter().GetResult(); }).Execute(action); } finally { isolatedContext.Dispose(); } }