31a8776e5b86e09aba774c07951acbf2163add33
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / OperationLimiterTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.access.concepts.MemberName;
13 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
15 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
16
17 /**
18  * Unit tests for OperationCompleter.
19  *
20  * @author Thomas Pantelis
21  */
22 public class OperationLimiterTest {
23
24     @Test
25     public void testOnComplete() throws Exception {
26         int permits = 10;
27         OperationLimiter limiter = new OperationLimiter(new TransactionIdentifier(MemberName.forName("foo"), 1), permits, 1);
28         limiter.acquire(permits);
29         int availablePermits = 0;
30
31         limiter.onComplete(null, new DataExistsReply(true, DataStoreVersions.CURRENT_VERSION));
32         assertEquals("availablePermits", ++availablePermits, limiter.availablePermits());
33
34         limiter.onComplete(null, new DataExistsReply(true, DataStoreVersions.CURRENT_VERSION));
35         assertEquals("availablePermits", ++availablePermits, limiter.availablePermits());
36
37         limiter.onComplete(null, new IllegalArgumentException());
38         assertEquals("availablePermits", ++availablePermits, limiter.availablePermits());
39
40         limiter.onComplete(null, new BatchedModificationsReply(4));
41         availablePermits += 4;
42         assertEquals("availablePermits", availablePermits, limiter.availablePermits());
43     }
44
45 }