/**
 * Copyright 2016 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * 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.palantir.atlasdb.keyvalue.dbkvs.impl;

import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;

import com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OverflowSequenceSupplier;
import com.palantir.nexus.db.sql.SqlConnection;

public class OverflowSequenceSupplierTest {

    @Test
    public void shouldGetConsecutiveOverflowIdsFromSameSupplier() throws Exception {
        final ConnectionSupplier conns = mock(ConnectionSupplier.class);
        final SqlConnection sqlConnection = mock(SqlConnection.class);

        when(conns.get()).thenReturn(sqlConnection);
        when(sqlConnection.selectIntegerUnregisteredQuery(anyString())).thenReturn(1);

        OverflowSequenceSupplier sequenceSupplier = OverflowSequenceSupplier.create(conns, "a_");
        long firstSequenceId = sequenceSupplier.get();
        long nextSequenceId = sequenceSupplier.get();

        assertThat(nextSequenceId - firstSequenceId, is(1L));
    }

    @Test
    public void shouldNotGetOverflowIdsWithOverlappingCachesFromDifferentSuppliers() throws Exception {
        final ConnectionSupplier conns = mock(ConnectionSupplier.class);
        final SqlConnection sqlConnection = mock(SqlConnection.class);

        when(conns.get()).thenReturn(sqlConnection);
        when(sqlConnection.selectIntegerUnregisteredQuery(anyString())).thenReturn(1, 1001);

        long firstSequenceId = OverflowSequenceSupplier.create(conns, "a_").get();
        long secondSequenceId = OverflowSequenceSupplier.create(conns, "a_").get();

        assertThat(secondSequenceId - firstSequenceId, greaterThanOrEqualTo(1000L));
    }
}

@@ -633,16 +633,61 @@
 .impl;%0A%0A
+import static org.hamcrest.Matchers.equalTo;%0A
 import s
@@ -2467,11 +2467,928 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldSkipValuesReservedByOtherSupplier() throws Exception %7B%0A        final ConnectionSupplier conns = mock(ConnectionSupplier.class);%0A        final SqlConnection sqlConnection = mock(SqlConnection.class);%0A%0A        when(conns.get()).thenReturn(sqlConnection);%0A        when(sqlConnection.selectIntegerUnregisteredQuery(anyString())).thenReturn(1, 1001, 2001);%0A%0A        OverflowSequenceSupplier firstSupplier = OverflowSequenceSupplier.create(conns, %22a_%22);%0A        firstSupplier.get(); // gets 1%0A        OverflowSequenceSupplier.create(conns, %22a_%22).get(); // gets 1001%0A%0A        // After 1000 gets from the first supplier, we should get to 1000%0A        long id = 0;%0A        for (int i = 0; i %3C 999; i++) %7B%0A            id = firstSupplier.get();%0A        %7D%0A        assertThat(id, equalTo(1000L));%0A%0A        // Should then skip to 2001%0A        assertThat(firstSupplier.get(), equalTo(2001L));%0A    %7D
 %0A%7D%0A

Add test for multiple suppliers not clashing