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