|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Net; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Microsoft.Spark.Interop.Ipc; |
| 14 | +using Microsoft.Spark.Network; |
| 15 | +using Moq; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace Microsoft.Spark.UnitTest |
| 19 | +{ |
| 20 | + [Collection("Spark Unit Tests")] |
| 21 | + public class CallbackTests |
| 22 | + { |
| 23 | + private readonly Mock<IJvmBridge> _mockJvm; |
| 24 | + |
| 25 | + public CallbackTests(SparkFixture fixture) |
| 26 | + { |
| 27 | + _mockJvm = fixture.MockJvm; |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public async Task TestCallbackIds() |
| 32 | + { |
| 33 | + int numToRegister = 100; |
| 34 | + var callbackServer = new CallbackServer(_mockJvm.Object, false); |
| 35 | + var callbackHandler = new TestCallbackHandler(); |
| 36 | + |
| 37 | + var ids = new ConcurrentBag<int>(); |
| 38 | + var tasks = new List<Task>(); |
| 39 | + for (int i = 0; i < numToRegister; ++i) |
| 40 | + { |
| 41 | + tasks.Add( |
| 42 | + Task.Run(() => ids.Add(callbackServer.RegisterCallback(callbackHandler)))); |
| 43 | + } |
| 44 | + |
| 45 | + await Task.WhenAll(tasks); |
| 46 | + |
| 47 | + IOrderedEnumerable<int> actualIds = ids.OrderBy(i => i); |
| 48 | + IEnumerable<int> expectedIds = Enumerable.Range(1, numToRegister); |
| 49 | + Assert.True(expectedIds.SequenceEqual(actualIds)); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void TestCallbackServer() |
| 54 | + { |
| 55 | + var callbackServer = new CallbackServer(_mockJvm.Object, false); |
| 56 | + var callbackHandler = new TestCallbackHandler(); |
| 57 | + |
| 58 | + callbackHandler.Id = callbackServer.RegisterCallback(callbackHandler); |
| 59 | + Assert.Equal(1, callbackHandler.Id); |
| 60 | + |
| 61 | + using ISocketWrapper callbackSocket = SocketFactory.CreateSocket(); |
| 62 | + callbackServer.Run(callbackSocket); |
| 63 | + |
| 64 | + int connectionNumber = 10; |
| 65 | + for (int i = 0; i < connectionNumber; ++i) |
| 66 | + { |
| 67 | + var ipEndpoint = (IPEndPoint)callbackSocket.LocalEndPoint; |
| 68 | + ISocketWrapper clientSocket = SocketFactory.CreateSocket(); |
| 69 | + clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port); |
| 70 | + |
| 71 | + WriteAndReadTestData(clientSocket, callbackHandler, i, new CancellationToken()); |
| 72 | + } |
| 73 | + |
| 74 | + Assert.Equal(connectionNumber, callbackServer.CurrentNumConnections); |
| 75 | + |
| 76 | + IOrderedEnumerable<int> actualValues = callbackHandler.Inputs.OrderBy(i => i); |
| 77 | + IEnumerable<int> expectedValues = Enumerable |
| 78 | + .Range(0, connectionNumber) |
| 79 | + .Select(i => callbackHandler.Apply(i)) |
| 80 | + .OrderBy(i => i); |
| 81 | + Assert.True(expectedValues.SequenceEqual(actualValues)); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void TestCallbackHandlers() |
| 86 | + { |
| 87 | + var tokenSource = new CancellationTokenSource(); |
| 88 | + var callbackHandlersDict = new ConcurrentDictionary<int, ICallbackHandler>(); |
| 89 | + int inputToHandler = 1; |
| 90 | + { |
| 91 | + // Test CallbackConnection using a ICallbackHandler that runs |
| 92 | + // normally without error. |
| 93 | + var callbackHandler = new TestCallbackHandler |
| 94 | + { |
| 95 | + Id = 1 |
| 96 | + }; |
| 97 | + callbackHandlersDict[callbackHandler.Id] = callbackHandler; |
| 98 | + TestCallbackConnection( |
| 99 | + callbackHandlersDict, |
| 100 | + callbackHandler, |
| 101 | + inputToHandler, |
| 102 | + tokenSource.Token); |
| 103 | + Assert.Single(callbackHandler.Inputs); |
| 104 | + Assert.Equal( |
| 105 | + callbackHandler.Apply(inputToHandler), |
| 106 | + callbackHandler.Inputs.First()); |
| 107 | + } |
| 108 | + { |
| 109 | + // Test CallbackConnection using a ICallbackHandler that |
| 110 | + // throws an exception. |
| 111 | + var callbackHandler = new ThrowsExceptionHandler |
| 112 | + { |
| 113 | + Id = 2 |
| 114 | + }; |
| 115 | + callbackHandlersDict[callbackHandler.Id] = callbackHandler; |
| 116 | + TestCallbackConnection( |
| 117 | + callbackHandlersDict, |
| 118 | + callbackHandler, |
| 119 | + inputToHandler, |
| 120 | + tokenSource.Token); |
| 121 | + Assert.Empty(callbackHandler.Inputs); |
| 122 | + } |
| 123 | + { |
| 124 | + // Test CallbackConnection when cancellation has been requested for the token. |
| 125 | + tokenSource.Cancel(); |
| 126 | + var callbackHandler = new TestCallbackHandler |
| 127 | + { |
| 128 | + Id = 3 |
| 129 | + }; |
| 130 | + callbackHandlersDict[callbackHandler.Id] = callbackHandler; |
| 131 | + TestCallbackConnection( |
| 132 | + callbackHandlersDict, |
| 133 | + callbackHandler, |
| 134 | + inputToHandler, |
| 135 | + tokenSource.Token); |
| 136 | + Assert.Empty(callbackHandler.Inputs); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void TestCallbackConnection( |
| 141 | + ConcurrentDictionary<int, ICallbackHandler> callbackHandlersDict, |
| 142 | + ITestCallbackHandler callbackHandler, |
| 143 | + int inputToHandler, |
| 144 | + CancellationToken token) |
| 145 | + { |
| 146 | + using ISocketWrapper serverListener = SocketFactory.CreateSocket(); |
| 147 | + serverListener.Listen(); |
| 148 | + |
| 149 | + var ipEndpoint = (IPEndPoint)serverListener.LocalEndPoint; |
| 150 | + ISocketWrapper clientSocket = SocketFactory.CreateSocket(); |
| 151 | + clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port); |
| 152 | + |
| 153 | + var callbackConnection = new CallbackConnection(0, clientSocket, callbackHandlersDict); |
| 154 | + Task.Run(() => callbackConnection.Run(token)); |
| 155 | + |
| 156 | + using ISocketWrapper serverSocket = serverListener.Accept(); |
| 157 | + WriteAndReadTestData(serverSocket, callbackHandler, inputToHandler, token); |
| 158 | + } |
| 159 | + |
| 160 | + private void WriteAndReadTestData( |
| 161 | + ISocketWrapper socket, |
| 162 | + ITestCallbackHandler callbackHandler, |
| 163 | + int inputToHandler, |
| 164 | + CancellationToken token) |
| 165 | + { |
| 166 | + Stream inputStream = socket.InputStream; |
| 167 | + Stream outputStream = socket.OutputStream; |
| 168 | + |
| 169 | + SerDe.Write(outputStream, (int)CallbackFlags.CALLBACK); |
| 170 | + SerDe.Write(outputStream, callbackHandler.Id); |
| 171 | + SerDe.Write(outputStream, sizeof(int)); |
| 172 | + SerDe.Write(outputStream, inputToHandler); |
| 173 | + SerDe.Write(outputStream, (int)CallbackFlags.END_OF_STREAM); |
| 174 | + outputStream.Flush(); |
| 175 | + |
| 176 | + if (token.IsCancellationRequested) |
| 177 | + { |
| 178 | + Assert.Throws<IOException>(() => SerDe.ReadInt32(inputStream)); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + int callbackFlag = SerDe.ReadInt32(inputStream); |
| 183 | + if (callbackFlag == (int)CallbackFlags.DOTNET_EXCEPTION_THROWN) |
| 184 | + { |
| 185 | + string exceptionMessage = SerDe.ReadString(inputStream); |
| 186 | + Assert.False(string.IsNullOrEmpty(exceptionMessage)); |
| 187 | + Assert.Contains(callbackHandler.ExceptionMessage, exceptionMessage); |
| 188 | + } |
| 189 | + else |
| 190 | + { |
| 191 | + Assert.Equal((int)CallbackFlags.END_OF_STREAM, callbackFlag); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private class TestCallbackHandler : ICallbackHandler, ITestCallbackHandler |
| 197 | + { |
| 198 | + public void Run(Stream inputStream) => Inputs.Add(Apply(SerDe.ReadInt32(inputStream))); |
| 199 | + |
| 200 | + public ConcurrentBag<int> Inputs { get; } = new ConcurrentBag<int>(); |
| 201 | + |
| 202 | + public int Id { get; set; } |
| 203 | + |
| 204 | + public bool Throws { get; } = false; |
| 205 | + |
| 206 | + public string ExceptionMessage => throw new NotImplementedException(); |
| 207 | + |
| 208 | + public int Apply(int i) => 10 * i; |
| 209 | + } |
| 210 | + |
| 211 | + private class ThrowsExceptionHandler : ICallbackHandler, ITestCallbackHandler |
| 212 | + { |
| 213 | + public void Run(Stream inputStream) => throw new Exception(ExceptionMessage); |
| 214 | + |
| 215 | + public ConcurrentBag<int> Inputs { get; } = new ConcurrentBag<int>(); |
| 216 | + |
| 217 | + public int Id { get; set; } |
| 218 | + |
| 219 | + public bool Throws { get; } = true; |
| 220 | + |
| 221 | + public string ExceptionMessage { get; } = "Dotnet Callback Handler Exception Message"; |
| 222 | + |
| 223 | + public int Apply(int i) => throw new NotImplementedException(); |
| 224 | + } |
| 225 | + |
| 226 | + private interface ITestCallbackHandler |
| 227 | + { |
| 228 | + ConcurrentBag<int> Inputs { get; } |
| 229 | + |
| 230 | + int Id { get; set; } |
| 231 | + |
| 232 | + bool Throws { get; } |
| 233 | + |
| 234 | + string ExceptionMessage { get; } |
| 235 | + |
| 236 | + int Apply(int i); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments