//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 com.cloud.network.nicira;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

public class ExecutionCounterTest {

    @Test
    public void testIncrementCounter() throws Exception {
        final ExecutionCounter executionCounter = new ExecutionCounter(-1);

        executionCounter.incrementExecutionCounter().incrementExecutionCounter();

        assertThat(executionCounter.getValue(), equalTo(2));
    }

    @Test
    public void testHasNotYetReachedTheExecutuionLimit() throws Exception {
        final ExecutionCounter executionCounter = new ExecutionCounter(2);

        executionCounter.incrementExecutionCounter();

        assertThat(executionCounter.hasReachedExecutionLimit(), equalTo(false));
    }

    @Test
    public void testHasAlreadyReachedTheExecutuionLimit() throws Exception {
        final ExecutionCounter executionCounter = new ExecutionCounter(2);

        executionCounter.incrementExecutionCounter().incrementExecutionCounter();

        assertThat(executionCounter.hasReachedExecutionLimit(), equalTo(true));
    }
}

@@ -935,16 +935,151 @@
 ualTo;%0A%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import o
@@ -2063,11 +2063,1606 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testConcurrentUpdatesToCounter() throws Exception %7B%0A        final ExecutionCounter executionCounter = new ExecutionCounter(0);%0A        final ExecutorService executorService = Executors.newFixedThreadPool(3);%0A        final AtomicInteger counterTask1 = new AtomicInteger(-1);%0A        final AtomicInteger counterTask2 = new AtomicInteger(-1);%0A        final AtomicInteger counterTask3 = new AtomicInteger(-1);%0A%0A        final Runnable task1 = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                executionCounter.incrementExecutionCounter().incrementExecutionCounter();%0A                executionCounter.incrementExecutionCounter().incrementExecutionCounter();%0A                counterTask1.set(executionCounter.getValue());%0A            %7D%0A        %7D;%0A        final Runnable task2 = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                executionCounter.incrementExecutionCounter().incrementExecutionCounter();%0A                counterTask2.set(executionCounter.getValue());%0A            %7D%0A        %7D;%0A        final Runnable task3 = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                counterTask3.set(executionCounter.getValue());%0A            %7D%0A        %7D;%0A%0A        executorService.execute(task1);%0A        executorService.execute(task2);%0A        executorService.execute(task3);%0A%0A        executorService.shutdown();%0A%0A        assertThat(counterTask1.get(), equalTo(4));%0A        assertThat(counterTask2.get(), equalTo(2));%0A        assertThat(counterTask3.get(), equalTo(0));%0A    %7D
 %0A%7D%0A

Add test to check that each thread has it's own execution counter