alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Sets; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 24 | import org.hamcrest.TypeSafeMatcher; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.TestApplicationId; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.impl.TestCoreManager; |
| 31 | import org.onosproject.event.impl.TestEventDispatcher; |
| 32 | import org.onosproject.net.NetworkResource; |
| 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 35 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 36 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentCompiler; |
| 39 | import org.onosproject.net.intent.IntentEvent; |
| 40 | import org.onosproject.net.intent.IntentEvent.Type; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.IntentId; |
| 43 | import org.onosproject.net.intent.IntentInstaller; |
| 44 | import org.onosproject.net.intent.IntentListener; |
| 45 | import org.onosproject.net.intent.IntentService; |
| 46 | import org.onosproject.net.intent.IntentState; |
| 47 | import org.onosproject.net.intent.IntentTestsMocks; |
| 48 | import org.onosproject.net.resource.LinkResourceAllocations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.store.trivial.impl.SimpleIntentStore; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 50 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 51 | import java.util.Collection; |
| 52 | import java.util.Collections; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
| 55 | import java.util.Set; |
| 56 | import java.util.concurrent.CountDownLatch; |
| 57 | import java.util.concurrent.TimeUnit; |
| 58 | import java.util.concurrent.atomic.AtomicLong; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 59 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 60 | import static org.hamcrest.MatcherAssert.assertThat; |
| 61 | import static org.hamcrest.Matchers.hasSize; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 62 | import static org.junit.Assert.*; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 63 | import static org.onlab.util.Tools.delay; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 64 | import static org.onosproject.net.intent.IntentState.*; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Test intent manager and transitions. |
| 68 | * |
| 69 | * TODO implement the following tests: |
| 70 | * - {submit, withdraw, update, replace} intent |
Sho SHIMIZU | 3466096 | 2015-01-22 17:58:44 -0800 | [diff] [blame] | 71 | * - {submit, update, recompiling} intent with failed compilation |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 72 | * - failed reservation |
| 73 | * - push timeout recovery |
| 74 | * - failed items recovery |
| 75 | * |
| 76 | * in general, verify intents store, flow store, and work queue |
| 77 | */ |
| 78 | public class IntentManagerTest { |
| 79 | |
| 80 | private static final ApplicationId APPID = new TestApplicationId("manager-test"); |
| 81 | |
| 82 | private IntentManager manager; |
| 83 | private MockFlowRuleService flowRuleService; |
| 84 | |
| 85 | protected IntentService service; |
| 86 | protected IntentExtensionService extensionService; |
| 87 | protected TestListener listener = new TestListener(); |
| 88 | protected TestIntentCompiler compiler = new TestIntentCompiler(); |
| 89 | protected TestIntentInstaller installer = new TestIntentInstaller(); |
| 90 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 91 | private static class TestListener implements IntentListener { |
| 92 | final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create(); |
| 93 | Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap(); |
| 94 | |
| 95 | @Override |
| 96 | public void event(IntentEvent event) { |
| 97 | events.put(event.type(), event); |
| 98 | if (latchMap.containsKey(event.type())) { |
| 99 | latchMap.get(event.type()).countDown(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public int getCounts(IntentEvent.Type type) { |
| 104 | return events.get(type).size(); |
| 105 | } |
| 106 | |
| 107 | public void setLatch(int count, IntentEvent.Type type) { |
| 108 | latchMap.put(type, new CountDownLatch(count)); |
| 109 | } |
| 110 | |
| 111 | public void await(IntentEvent.Type type) { |
| 112 | try { |
| 113 | assertTrue("Timed out waiting for: " + type, |
| 114 | latchMap.get(type).await(5, TimeUnit.SECONDS)); |
| 115 | } catch (InterruptedException e) { |
| 116 | e.printStackTrace(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private static class TestIntentTracker implements ObjectiveTrackerService { |
| 122 | private TopologyChangeDelegate delegate; |
| 123 | @Override |
| 124 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 125 | this.delegate = delegate; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 130 | if (delegate.equals(this.delegate)) { |
| 131 | this.delegate = null; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void addTrackedResources(IntentId intentId, Collection<NetworkResource> resources) { |
| 137 | //TODO |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void removeTrackedResources(IntentId intentId, Collection<NetworkResource> resources) { |
| 142 | //TODO |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private static class MockIntent extends Intent { |
| 147 | private static AtomicLong counter = new AtomicLong(0); |
| 148 | |
| 149 | private final Long number; |
| 150 | // Nothing new here |
| 151 | public MockIntent(Long number) { |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 152 | super(APPID, Collections.emptyList()); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 153 | this.number = number; |
| 154 | } |
| 155 | |
| 156 | public Long number() { |
| 157 | return number; |
| 158 | } |
| 159 | |
| 160 | public static Long nextId() { |
| 161 | return counter.getAndIncrement(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private static class MockInstallableIntent extends MockIntent { |
| 166 | public MockInstallableIntent(Long number) { |
| 167 | super(number); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public boolean isInstallable() { |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private static class TestIntentCompiler implements IntentCompiler<MockIntent> { |
| 177 | @Override |
| 178 | public List<Intent> compile(MockIntent intent, List<Intent> installable, |
| 179 | Set<LinkResourceAllocations> resources) { |
| 180 | return Lists.newArrayList(new MockInstallableIntent(intent.number())); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private static class TestIntentCompilerError implements IntentCompiler<MockIntent> { |
| 185 | @Override |
| 186 | public List<Intent> compile(MockIntent intent, List<Intent> installable, |
| 187 | Set<LinkResourceAllocations> resources) { |
| 188 | throw new IntentCompilationException("Compilation always fails"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> { |
| 193 | @Override |
| 194 | public List<FlowRuleBatchOperation> install(MockInstallableIntent intent) { |
| 195 | FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue()); |
| 196 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 197 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, fr)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 198 | return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0)); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public List<FlowRuleBatchOperation> uninstall(MockInstallableIntent intent) { |
| 203 | FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue()); |
| 204 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 205 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, fr)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 206 | return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0)); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public List<FlowRuleBatchOperation> replace(MockInstallableIntent oldIntent, MockInstallableIntent newIntent) { |
| 211 | FlowRule fr = new IntentTestsMocks.MockFlowRule(oldIntent.number().intValue()); |
| 212 | FlowRule fr2 = new IntentTestsMocks.MockFlowRule(newIntent.number().intValue()); |
| 213 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 214 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, fr)); |
| 215 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, fr2)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 216 | return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0)); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | private static class TestIntentErrorInstaller implements IntentInstaller<MockInstallableIntent> { |
| 221 | @Override |
| 222 | public List<FlowRuleBatchOperation> install(MockInstallableIntent intent) { |
| 223 | throw new IntentInstallationException("install() always fails"); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public List<FlowRuleBatchOperation> uninstall(MockInstallableIntent intent) { |
| 228 | throw new IntentRemovalException("uninstall() always fails"); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public List<FlowRuleBatchOperation> replace(MockInstallableIntent oldIntent, MockInstallableIntent newIntent) { |
| 233 | throw new IntentInstallationException("replace() always fails"); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Hamcrest matcher to check that a conllection of Intents contains an |
| 239 | * Intent with the specified Intent Id. |
| 240 | */ |
| 241 | public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> { |
| 242 | private final IntentId id; |
| 243 | |
| 244 | public EntryForIntentMatcher(IntentId idValue) { |
| 245 | id = idValue; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public boolean matchesSafely(Collection<Intent> intents) { |
| 250 | for (Intent intent : intents) { |
| 251 | if (intent.id().equals(id)) { |
| 252 | return true; |
| 253 | } |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void describeTo(Description description) { |
| 260 | description.appendText("an intent with id \" "). |
| 261 | appendText(id.toString()). |
| 262 | appendText("\""); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private static EntryForIntentMatcher hasIntentWithId(IntentId id) { |
| 267 | return new EntryForIntentMatcher(id); |
| 268 | } |
| 269 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 270 | @Before |
| 271 | public void setUp() { |
| 272 | manager = new IntentManager(); |
| 273 | flowRuleService = new MockFlowRuleService(); |
| 274 | manager.store = new SimpleIntentStore(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 275 | manager.eventDispatcher = new TestEventDispatcher(); |
| 276 | manager.trackerService = new TestIntentTracker(); |
| 277 | manager.flowRuleService = flowRuleService; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 278 | manager.coreService = new TestCoreManager(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 279 | service = manager; |
| 280 | extensionService = manager; |
| 281 | |
| 282 | manager.activate(); |
| 283 | service.addListener(listener); |
| 284 | extensionService.registerCompiler(MockIntent.class, compiler); |
| 285 | extensionService.registerInstaller(MockInstallableIntent.class, installer); |
| 286 | |
| 287 | assertTrue("store should be empty", |
| 288 | Sets.newHashSet(service.getIntents()).isEmpty()); |
| 289 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 290 | } |
| 291 | |
| 292 | @After |
| 293 | public void tearDown() { |
| 294 | // verify that all intents are parked and the batch operation is unblocked |
| 295 | Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED); |
| 296 | for (Intent i : service.getIntents()) { |
| 297 | IntentState state = service.getIntentState(i.id()); |
| 298 | assertTrue("Intent " + i.id() + " is in invalid state " + state, |
| 299 | parked.contains(state)); |
| 300 | } |
| 301 | //the batch has not yet been removed when we receive the last event |
| 302 | // FIXME: this doesn't guarantee to avoid the race |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 303 | |
| 304 | //FIXME |
| 305 | // for (int tries = 0; tries < 10; tries++) { |
| 306 | // if (manager.batchService.getPendingOperations().isEmpty()) { |
| 307 | // break; |
| 308 | // } |
| 309 | // delay(10); |
| 310 | // } |
| 311 | // assertTrue("There are still pending batch operations.", |
| 312 | // manager.batchService.getPendingOperations().isEmpty()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 313 | |
| 314 | extensionService.unregisterCompiler(MockIntent.class); |
| 315 | extensionService.unregisterInstaller(MockInstallableIntent.class); |
| 316 | service.removeListener(listener); |
| 317 | manager.deactivate(); |
| 318 | // TODO null the other refs? |
| 319 | } |
| 320 | |
| 321 | @Test |
| 322 | public void submitIntent() { |
| 323 | flowRuleService.setFuture(true); |
| 324 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 325 | listener.setLatch(1, Type.INSTALL_REQ); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 326 | listener.setLatch(1, Type.INSTALLED); |
| 327 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 328 | service.submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 329 | listener.await(Type.INSTALL_REQ); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 330 | listener.await(Type.INSTALLED); |
| 331 | assertEquals(1L, service.getIntentCount()); |
| 332 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
| 333 | } |
| 334 | |
| 335 | @Test |
| 336 | public void withdrawIntent() { |
| 337 | flowRuleService.setFuture(true); |
| 338 | |
| 339 | listener.setLatch(1, Type.INSTALLED); |
| 340 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 341 | service.submit(intent); |
| 342 | listener.await(Type.INSTALLED); |
| 343 | assertEquals(1L, service.getIntentCount()); |
| 344 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
| 345 | |
| 346 | listener.setLatch(1, Type.WITHDRAWN); |
| 347 | service.withdraw(intent); |
| 348 | listener.await(Type.WITHDRAWN); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 349 | delay(10); //FIXME this is a race |
| 350 | assertEquals(0L, service.getIntentCount()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 351 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 352 | } |
| 353 | |
| 354 | @Test |
| 355 | public void stressSubmitWithdraw() { |
| 356 | flowRuleService.setFuture(true); |
| 357 | |
| 358 | int count = 500; |
| 359 | |
| 360 | listener.setLatch(count, Type.INSTALLED); |
| 361 | listener.setLatch(count, Type.WITHDRAWN); |
| 362 | |
| 363 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 364 | for (int i = 0; i < count; i++) { |
| 365 | service.submit(intent); |
| 366 | service.withdraw(intent); |
| 367 | } |
| 368 | |
| 369 | listener.await(Type.INSTALLED); |
| 370 | listener.await(Type.WITHDRAWN); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 371 | delay(10); //FIXME this is a race |
| 372 | assertEquals(0L, service.getIntentCount()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 373 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 374 | } |
| 375 | |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 376 | /** |
| 377 | * Tests for proper behavior of installation of an intent that triggers |
| 378 | * a compilation error. |
| 379 | */ |
| 380 | @Test |
| 381 | public void errorIntentCompile() { |
| 382 | final TestIntentCompilerError errorCompiler = new TestIntentCompilerError(); |
| 383 | extensionService.registerCompiler(MockIntent.class, errorCompiler); |
| 384 | MockIntent intent = new MockIntent(MockIntent.nextId()); |
| 385 | listener.setLatch(1, Type.INSTALL_REQ); |
| 386 | listener.setLatch(1, Type.FAILED); |
| 387 | service.submit(intent); |
| 388 | listener.await(Type.INSTALL_REQ); |
| 389 | listener.await(Type.FAILED); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Tests handling a future that contains an error as a result of |
| 394 | * installing an intent. |
| 395 | */ |
| 396 | @Test |
| 397 | public void errorIntentInstallFromFlows() { |
| 398 | final Long id = MockIntent.nextId(); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame^] | 399 | flowRuleService.setFuture(false); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 400 | MockIntent intent = new MockIntent(id); |
| 401 | listener.setLatch(1, Type.FAILED); |
| 402 | listener.setLatch(1, Type.INSTALL_REQ); |
| 403 | service.submit(intent); |
| 404 | listener.await(Type.INSTALL_REQ); |
| 405 | delay(10); // need to make sure we have some failed futures returned first |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame^] | 406 | flowRuleService.setFuture(true); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 407 | listener.await(Type.FAILED); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Tests handling of an error that is generated by the intent installer. |
| 412 | */ |
| 413 | @Test |
| 414 | public void errorIntentInstallFromInstaller() { |
| 415 | final TestIntentErrorInstaller errorInstaller = new TestIntentErrorInstaller(); |
| 416 | extensionService.registerInstaller(MockInstallableIntent.class, errorInstaller); |
| 417 | MockIntent intent = new MockIntent(MockIntent.nextId()); |
| 418 | listener.setLatch(1, Type.INSTALL_REQ); |
| 419 | listener.setLatch(1, Type.FAILED); |
| 420 | service.submit(intent); |
| 421 | listener.await(Type.INSTALL_REQ); |
| 422 | listener.await(Type.FAILED); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 425 | /** |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 426 | * Tests handling a future that contains an unresolvable error as a result of |
| 427 | * installing an intent. |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 428 | */ |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 429 | @Test |
| 430 | public void errorIntentInstallNeverTrue() { |
| 431 | final Long id = MockIntent.nextId(); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame^] | 432 | flowRuleService.setFuture(false); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 433 | MockIntent intent = new MockIntent(id); |
| 434 | listener.setLatch(1, Type.WITHDRAWN); |
| 435 | listener.setLatch(1, Type.INSTALL_REQ); |
| 436 | service.submit(intent); |
| 437 | listener.await(Type.INSTALL_REQ); |
| 438 | // The delay here forces the retry loop in the intent manager to time out |
| 439 | delay(100); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame^] | 440 | flowRuleService.setFuture(false); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 441 | service.withdraw(intent); |
| 442 | listener.await(Type.WITHDRAWN); |
| 443 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 444 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 445 | /** |
| 446 | * Tests that a compiler for a subclass of an intent that already has a |
| 447 | * compiler is automatically added. |
| 448 | */ |
| 449 | @Test |
| 450 | public void intentSubclassCompile() { |
| 451 | class MockIntentSubclass extends MockIntent { |
| 452 | public MockIntentSubclass(Long number) { |
| 453 | super(number); |
| 454 | } |
| 455 | } |
| 456 | flowRuleService.setFuture(true); |
| 457 | |
| 458 | listener.setLatch(1, Type.INSTALL_REQ); |
| 459 | listener.setLatch(1, Type.INSTALLED); |
| 460 | Intent intent = new MockIntentSubclass(MockIntent.nextId()); |
| 461 | service.submit(intent); |
| 462 | listener.await(Type.INSTALL_REQ); |
| 463 | listener.await(Type.INSTALLED); |
| 464 | assertEquals(1L, service.getIntentCount()); |
| 465 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
| 466 | |
| 467 | final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = |
| 468 | extensionService.getCompilers(); |
| 469 | assertEquals(2, compilers.size()); |
| 470 | assertNotNull(compilers.get(MockIntentSubclass.class)); |
| 471 | assertNotNull(compilers.get(MockIntent.class)); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Tests an intent with no compiler. |
| 476 | */ |
| 477 | @Test |
| 478 | public void intentWithoutCompiler() { |
| 479 | class IntentNoCompiler extends Intent { |
| 480 | IntentNoCompiler() { |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 481 | super(APPID, Collections.emptyList()); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 482 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 485 | Intent intent = new IntentNoCompiler(); |
| 486 | listener.setLatch(1, Type.INSTALL_REQ); |
| 487 | listener.setLatch(1, Type.FAILED); |
| 488 | service.submit(intent); |
| 489 | listener.await(Type.INSTALL_REQ); |
| 490 | listener.await(Type.FAILED); |
| 491 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 492 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 493 | /** |
| 494 | * Tests an intent with no installer. |
| 495 | */ |
| 496 | @Test |
| 497 | public void intentWithoutInstaller() { |
| 498 | |
| 499 | extensionService.unregisterInstaller(MockInstallableIntent.class); |
| 500 | |
| 501 | MockIntent intent = new MockIntent(MockIntent.nextId()); |
| 502 | listener.setLatch(1, Type.INSTALL_REQ); |
| 503 | listener.setLatch(1, Type.FAILED); |
| 504 | service.submit(intent); |
| 505 | listener.await(Type.INSTALL_REQ); |
| 506 | listener.await(Type.FAILED); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Tests that the intent fetching methods are correct. |
| 511 | */ |
| 512 | @Test |
| 513 | public void testIntentFetching() { |
| 514 | List<Intent> intents; |
| 515 | |
| 516 | flowRuleService.setFuture(true); |
| 517 | |
| 518 | intents = Lists.newArrayList(service.getIntents()); |
| 519 | assertThat(intents, hasSize(0)); |
| 520 | |
| 521 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 522 | final MockIntent intent2 = new MockIntent(MockIntent.nextId()); |
| 523 | |
| 524 | listener.setLatch(2, Type.INSTALL_REQ); |
| 525 | listener.setLatch(2, Type.INSTALLED); |
| 526 | service.submit(intent1); |
| 527 | service.submit(intent2); |
| 528 | listener.await(Type.INSTALL_REQ); |
| 529 | listener.await(Type.INSTALL_REQ); |
| 530 | listener.await(Type.INSTALLED); |
| 531 | listener.await(Type.INSTALLED); |
| 532 | |
| 533 | intents = Lists.newArrayList(service.getIntents()); |
| 534 | assertThat(intents, hasSize(2)); |
| 535 | |
| 536 | assertThat(intents, hasIntentWithId(intent1.id())); |
| 537 | assertThat(intents, hasIntentWithId(intent2.id())); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 538 | } |
| 539 | } |