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