<commit_before>/*
 * Copyright 2015 the original author or authors.
 *
 * 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 io.atomix.concurrent;

import io.atomix.testing.AbstractCopycatTest;
import org.testng.annotations.Test;

import java.time.Duration;

/**
 * Async lock test.
 *
 * @author <a href="http://github.com/kuujo"><NAME></a>
 */
@Test
public class DistributedLockTest extends AbstractCopycatTest<DistributedLock> {
  
  @Override
  protected Class<? super DistributedLock> type() {
    return DistributedLock.class;
  }

  /**
   * Tests locking and unlocking a lock.
   */
  public void testLockUnlock() throws Throwable {
    createServers(3);

    DistributedLock lock = createResource();

    lock.lock().thenRun(this::resume);
    await(10000);

    lock.unlock().thenRun(this::resume);
    await(10000);
  }

  /**
   * Tests releasing a lock when the client's session is closed.
   */
  public void testReleaseOnClose() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(this::resume);
    await(10000);

    lock2.lock().thenRun(this::resume);
    lock1.close();
    await(10000);
  }

  /**
   * Tests attempting to acquire a lock with a timeout.
   */
  public void testTryLockFail() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(this::resume);
    await(10000);

    lock2.tryLock(Duration.ofSeconds(1)).thenAccept(result -> {
      threadAssertNull(result);
      resume();
    });
    await(10000);
  }

  /**
   * Tests unlocking a lock with a blocking call in the event thread.
   */
  public void testBlockingUnlock() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(() -> {
      lock1.unlock().join();
      resume();
    });

    lock2.lock().thenRun(this::resume);
    await(10000, 2);
  }

}<commit_msg>Fix race condition in blocking lock test.<commit_after>/*
 * Copyright 2015 the original author or authors.
 *
 * 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 io.atomix.concurrent;

import io.atomix.testing.AbstractCopycatTest;
import org.testng.annotations.Test;

import java.time.Duration;

/**
 * Async lock test.
 *
 * @author <a href="http://github.com/kuujo"><NAME></a>
 */
@Test
public class DistributedLockTest extends AbstractCopycatTest<DistributedLock> {
  
  @Override
  protected Class<? super DistributedLock> type() {
    return DistributedLock.class;
  }

  /**
   * Tests locking and unlocking a lock.
   */
  public void testLockUnlock() throws Throwable {
    createServers(3);

    DistributedLock lock = createResource();

    lock.lock().thenRun(this::resume);
    await(10000);

    lock.unlock().thenRun(this::resume);
    await(10000);
  }

  /**
   * Tests releasing a lock when the client's session is closed.
   */
  public void testReleaseOnClose() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(this::resume);
    await(10000);

    lock2.lock().thenRun(this::resume);
    lock1.close();
    await(10000);
  }

  /**
   * Tests attempting to acquire a lock with a timeout.
   */
  public void testTryLockFail() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(this::resume);
    await(10000);

    lock2.tryLock(Duration.ofSeconds(1)).thenAccept(result -> {
      threadAssertNull(result);
      resume();
    });
    await(10000);
  }

  /**
   * Tests unlocking a lock with a blocking call in the event thread.
   */
  public void testBlockingUnlock() throws Throwable {
    createServers(3);

    DistributedLock lock1 = createResource();
    DistributedLock lock2 = createResource();

    lock1.lock().thenRun(() -> {
      resume();
      lock1.unlock().join();
      resume();
    });
    await(10000);

    lock2.lock().thenRun(this::resume);
    await(10000, 2);
  }

}
{'fraction_non_alphanumeric': 0.11873804971319311, 'fraction_numerical': 0.025047801147227534, 'mean_word_length': 3.440577249575552, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3437413', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1507, 'n_words': 442}