Sho SHIMIZU
Committed by Gerrit Code Review

Fix bug in releasing a continuous resource

Change-Id: Idcc1faaf0ce9cd7856233dc2107dcc8329e6be94
......@@ -86,7 +86,7 @@ final class ContinuousResourceAllocation {
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
if (matched.size() > 1) {
if (matched.size() > 0) {
matched.remove(0);
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.resource.impl;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.onlab.util.Bandwidth;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.intent.IntentId;
import org.onosproject.net.resource.ContinuousResource;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.resource.ResourceConsumer;
import org.onosproject.net.resource.Resources;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Unit tests for ContinuousResourceAllocation.
*/
public class ContinuousResourceAllocationTest {
private static final DeviceId DID = DeviceId.deviceId("a");
private static final PortNumber PN1 = PortNumber.portNumber(1);
@Test
public void testReleaseWhenAllocatedResourceIsRequested() {
ContinuousResource original =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
ContinuousResource allocated =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ResourceConsumer consumer = IntentId.valueOf(1);
ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
ImmutableList.of(new ResourceAllocation(allocated, consumer)));
ContinuousResource request =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ContinuousResourceAllocation released = sut.release(request, consumer.consumerId());
assertThat(released.allocations().isEmpty(), is(true));
}
@Test
public void testReleaseWhenDifferentConsumerIsSpecified() {
ContinuousResource original =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
ContinuousResource allocated =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ResourceConsumer consumer = IntentId.valueOf(1);
ResourceConsumer otherConsumer = IntentId.valueOf(2);
ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
ImmutableList.of(new ResourceAllocation(allocated, consumer)));
ContinuousResource request =
Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
assertThat(allocations.size(), is(1));
assertThat(allocations.get(0).resource().equals(allocated), is(true));
}
}