Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
original-flipster69 committed Aug 26, 2024
1 parent 9b14521 commit ad4072a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Add the following dependency to your project:
- Maven
```xml
<dependency>
<groupId>com.originalflipster</groupId>
<artifactId>cloud-lock</artifactId>
<version>0.1.0</version>
<groupId>com.originalflipster</groupId>
<artifactId>cloud-lock</artifactId>
<version>0.0.7</version>
</dependency>
```
- Gradle
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<version>12.27.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-nio</artifactId>
<version>0.127.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/originalflipster/cloud/lock/StorageLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ final class StorageLock {
private static final long LIFETIME_MINUTES = 1L;
private static final long HEARTBEAT_SECONDS = 5L;

private final CloudStorage providerLock;
private final CloudStorage storage;

StorageLock(final CloudStorage cloudLock) {
this.providerLock = Objects.requireNonNull(cloudLock);
StorageLock(final CloudStorage storage) {
this.storage = Objects.requireNonNull(storage);
}

boolean acquireLock() {
if (!providerLock.lockFileExists()) {
var gotLock = providerLock.lock();
if (!storage.lockFileExists()) {
var gotLock = storage.lock();
if (!gotLock) {
LOG.info("failed acquiring lock");
return false;
Expand All @@ -34,13 +34,13 @@ boolean acquireLock() {
return true;
}

LocalDateTime lockTime = LocalDateTime.parse(providerLock.getLockContent());
LocalDateTime lockTime = LocalDateTime.parse(storage.getLockContent());
var minutesBetween = MINUTES.between(lockTime, LocalDateTime.now());

if (minutesBetween > LIFETIME_MINUTES) {
//FIXME check necessity
providerLock.deleteLock();
boolean gotLock = providerLock.lock();
storage.deleteLock();
boolean gotLock = storage.lock();
if (!gotLock) {
LOG.info("failed acquiring lock");
return false;
Expand All @@ -53,11 +53,11 @@ boolean acquireLock() {
}

void releaseLock() {
if (!providerLock.hasLock()) {
if (!storage.hasLock()) {
LOG.warn("trying to release lock, but no lock present");
return;
}
LocalDateTime lockTime = LocalDateTime.parse(providerLock.getLockContent());
LocalDateTime lockTime = LocalDateTime.parse(storage.getLockContent());
if (!LocalDateTime.now().isAfter(lockTime.plus(HEARTBEAT_SECONDS, SECONDS))) {
LOG.info("waiting minimum lock hold duration until releasing lock again...");
try {
Expand All @@ -67,8 +67,8 @@ void releaseLock() {
//
}
}
providerLock.deleteLock();
storage.deleteLock();
LOG.info("lock released");
providerLock.unlock();
storage.unlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
Expand Down Expand Up @@ -35,6 +36,15 @@ final class AlibabaObjectStorage implements CloudStorage {
this.accessKeySecret = accessKeySecret;
}

AlibabaObjectStorage(final String bucketName, final String lockFile, final String endpoint, final OSSClient ossClient) {
this.bucketName = bucketName;
this.lockFile = lockFile;
this.endpoint = endpoint;
this.ossClient = ossClient;
this.accessKeyId = "";
this.accessKeySecret = "";
}

private OSS getClient() {
if (ossClient == null) {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/originalflipster/cloud/lock/StorageLockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.originalflipster.cloud.lock;

import com.originalflipster.cloud.lock.storage.CloudStorage;

import static org.junit.jupiter.api.Assertions.*;

class StorageLockTest {


private static final class MockStorage implements CloudStorage {

@Override
public boolean lockFileExists() {
return false;
}

@Override
public boolean lock() {
return false;
}

@Override
public String getLockContent() {
return null;
}

@Override
public boolean hasLock() {
return false;
}

@Override
public void deleteLock() {

}

@Override
public void unlock() {

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.originalflipster.cloud.lock.storage;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
Expand All @@ -25,13 +25,13 @@ class AlibabaObjectStorageTest {
private static final String BUCKET_NAME = "test-bucket";
private static final String LOCK_FILE = "lock-file";

private OSS ossClient;
private OSSClient ossClient;
private AlibabaObjectStorage alibabaObjectStorage;

@BeforeEach
void setUp() {
ossClient = mock(OSS.class);
//alibabaObjectStorage = new AlibabaObjectStorage(BUCKET_NAME, LOCK_FILE, () -> ossClient);
ossClient = mock(OSSClient.class);
alibabaObjectStorage = new AlibabaObjectStorage(BUCKET_NAME, LOCK_FILE, "", ossClient);
}

@Test
Expand Down

0 comments on commit ad4072a

Please sign in to comment.