|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.network; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.concurrent.Semaphore; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import com.google.common.base.Charsets; |
| 28 | +import com.google.common.collect.Sets; |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import static org.junit.Assert.*; |
| 34 | + |
| 35 | +import org.apache.spark.network.client.RpcResponseCallback; |
| 36 | +import org.apache.spark.network.client.SluiceClient; |
| 37 | +import org.apache.spark.network.client.SluiceClientFactory; |
| 38 | +import org.apache.spark.network.server.DefaultStreamManager; |
| 39 | +import org.apache.spark.network.server.RpcHandler; |
| 40 | +import org.apache.spark.network.server.SluiceServer; |
| 41 | +import org.apache.spark.network.util.DefaultConfigProvider; |
| 42 | +import org.apache.spark.network.util.SluiceConfig; |
| 43 | + |
| 44 | +public class RpcIntegrationSuite { |
| 45 | + static SluiceServer server; |
| 46 | + static SluiceClientFactory clientFactory; |
| 47 | + static RpcHandler rpcHandler; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void setUp() throws Exception { |
| 51 | + SluiceConfig conf = new SluiceConfig(new DefaultConfigProvider()); |
| 52 | + rpcHandler = new RpcHandler() { |
| 53 | + @Override |
| 54 | + public void receive(SluiceClient client, byte[] message, RpcResponseCallback callback) { |
| 55 | + String msg = new String(message, Charsets.UTF_8); |
| 56 | + String[] parts = msg.split("/"); |
| 57 | + if (parts[0].equals("hello")) { |
| 58 | + callback.onSuccess(("Hello, " + parts[1] + "!").getBytes(Charsets.UTF_8)); |
| 59 | + } else if (parts[0].equals("return error")) { |
| 60 | + callback.onFailure(new RuntimeException("Returned: " + parts[1])); |
| 61 | + } else if (parts[0].equals("throw error")) { |
| 62 | + throw new RuntimeException("Thrown: " + parts[1]); |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + SluiceContext context = new SluiceContext(conf, new DefaultStreamManager(), rpcHandler); |
| 67 | + server = context.createServer(); |
| 68 | + clientFactory = context.createClientFactory(); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterClass |
| 72 | + public static void tearDown() { |
| 73 | + server.close(); |
| 74 | + clientFactory.close(); |
| 75 | + } |
| 76 | + |
| 77 | + class RpcResult { |
| 78 | + public Set<String> successMessages; |
| 79 | + public Set<String> errorMessages; |
| 80 | + } |
| 81 | + |
| 82 | + private RpcResult sendRPC(String ... commands) throws Exception { |
| 83 | + SluiceClient client = clientFactory.createClient(TestUtils.getLocalHost(), server.getPort()); |
| 84 | + final Semaphore sem = new Semaphore(0); |
| 85 | + |
| 86 | + final RpcResult res = new RpcResult(); |
| 87 | + res.successMessages = Collections.synchronizedSet(new HashSet<String>()); |
| 88 | + res.errorMessages = Collections.synchronizedSet(new HashSet<String>()); |
| 89 | + |
| 90 | + RpcResponseCallback callback = new RpcResponseCallback() { |
| 91 | + @Override |
| 92 | + public void onSuccess(byte[] message) { |
| 93 | + res.successMessages.add(new String(message, Charsets.UTF_8)); |
| 94 | + sem.release(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onFailure(Throwable e) { |
| 99 | + res.errorMessages.add(e.getMessage()); |
| 100 | + sem.release(); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + for (String command : commands) { |
| 105 | + client.sendRpc(command.getBytes(Charsets.UTF_8), callback); |
| 106 | + } |
| 107 | + |
| 108 | + if (!sem.tryAcquire(commands.length, 5, TimeUnit.SECONDS)) { |
| 109 | + fail("Timeout getting response from the server"); |
| 110 | + } |
| 111 | + client.close(); |
| 112 | + return res; |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void singleRPC() throws Exception { |
| 117 | + RpcResult res = sendRPC("hello/Aaron"); |
| 118 | + assertEquals(res.successMessages, Sets.newHashSet("Hello, Aaron!")); |
| 119 | + assertTrue(res.errorMessages.isEmpty()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void doubleRPC() throws Exception { |
| 124 | + RpcResult res = sendRPC("hello/Aaron", "hello/Reynold"); |
| 125 | + assertEquals(res.successMessages, Sets.newHashSet("Hello, Aaron!", "Hello, Reynold!")); |
| 126 | + assertTrue(res.errorMessages.isEmpty()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void returnErrorRPC() throws Exception { |
| 131 | + RpcResult res = sendRPC("return error/OK"); |
| 132 | + assertTrue(res.successMessages.isEmpty()); |
| 133 | + assertErrorsContain(res.errorMessages, Sets.newHashSet("Returned: OK")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void throwErrorRPC() throws Exception { |
| 138 | + RpcResult res = sendRPC("throw error/uh-oh"); |
| 139 | + assertTrue(res.successMessages.isEmpty()); |
| 140 | + assertErrorsContain(res.errorMessages, Sets.newHashSet("Thrown: uh-oh")); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void doubleTrouble() throws Exception { |
| 145 | + RpcResult res = sendRPC("return error/OK", "throw error/uh-oh"); |
| 146 | + assertTrue(res.successMessages.isEmpty()); |
| 147 | + assertErrorsContain(res.errorMessages, Sets.newHashSet("Returned: OK", "Thrown: uh-oh")); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void sendSuccessAndFailure() throws Exception { |
| 152 | + RpcResult res = sendRPC("hello/Bob", "throw error/the", "hello/Builder", "return error/!"); |
| 153 | + assertEquals(res.successMessages, Sets.newHashSet("Hello, Bob!", "Hello, Builder!")); |
| 154 | + assertErrorsContain(res.errorMessages, Sets.newHashSet("Thrown: the", "Returned: !")); |
| 155 | + } |
| 156 | + |
| 157 | + private void assertErrorsContain(Set<String> errors, Set<String> contains) { |
| 158 | + assertEquals(contains.size(), errors.size()); |
| 159 | + |
| 160 | + Set<String> remainingErrors = Sets.newHashSet(errors); |
| 161 | + for (String contain : contains) { |
| 162 | + Iterator<String> it = remainingErrors.iterator(); |
| 163 | + boolean foundMatch = false; |
| 164 | + while (it.hasNext()) { |
| 165 | + if (it.next().contains(contain)) { |
| 166 | + it.remove(); |
| 167 | + foundMatch = true; |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + assertTrue("Could not find error containing " + contain + "; errors: " + errors, foundMatch); |
| 172 | + } |
| 173 | + |
| 174 | + assertTrue(remainingErrors.isEmpty()); |
| 175 | + } |
| 176 | +} |
0 commit comments