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