Skip to content

Commit

Permalink
chore: lock demo (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: seeflood <[email protected]>
  • Loading branch information
akkw and seeflood authored Sep 26, 2022
1 parent 17527f9 commit c4e16f6
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 59 deletions.
78 changes: 78 additions & 0 deletions examples-lock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>runtime-sdk-parent</artifactId>
<groupId>io.mosn.layotto</groupId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>io.mosn.layotto</groupId>
<artifactId>examples-lock</artifactId>
<version>1.2.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<finalName>examples-lock</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>io.mosn.layotto.examples.lock.Lock</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2021 Layotto 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.mosn.layotto.examples.lock;

import io.mosn.layotto.v1.RuntimeClientBuilder;
import io.mosn.layotto.v1.config.RuntimeProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spec.sdk.runtime.v1.client.RuntimeClient;
import spec.sdk.runtime.v1.domain.lock.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class Lock {

private final static Logger logger = LoggerFactory.getLogger(Lock.class);
static String storeName = "lock_demo";
static String key1 = "key1";
static String key2 = "key2";
static String key3 = "key3";
static String owner1 = UUID.randomUUID().toString();
static String owner2 = UUID.randomUUID().toString();
static String owner3 = UUID.randomUUID().toString();

public static void main(String[] args) throws InterruptedException {
// build RuntimeClient
RuntimeClient client = new RuntimeClientBuilder()
.withPort(RuntimeProperties.DEFAULT_PORT)
.build();

// lock key1 successfully
TryLockRequest tryLockRequest = new TryLockRequest(storeName, key1, owner1, 5);
TryLockResponse tryLockResponse = client.tryLock(tryLockRequest);
assertTrue(tryLockResponse.isSuccess());
logger.info(tryLockResponse.toString());

// lock key2 successfully
TryLockRequest tryLockRequest1 = new TryLockRequest(storeName, key2, owner2, 5);
TryLockResponse tryLockResponse1 = client.tryLock(tryLockRequest1);
assertTrue(tryLockResponse1.isSuccess());
logger.info(tryLockResponse1.toString());

// lock key3 successfully
TryLockRequest tryLockRequest2 = new TryLockRequest(storeName, key3, owner3, 3);
TryLockResponse tryLockResponse2 = client.tryLock(tryLockRequest2);
assertTrue(tryLockResponse2.isSuccess());
logger.info(tryLockResponse2.toString());

// unlock key1 successfully
UnlockRequest unlockRequest = new UnlockRequest(storeName, key1, owner1);
UnlockResponse unlockResponse = client.unlock(unlockRequest);
assertEquals(unlockResponse.getStatus(), UnlockResponseStatus.SUCCESS);
logger.info(unlockResponse.toString());

// unlock key2 successfully
UnlockRequest unlockRequest1 = new UnlockRequest(storeName, key2, owner2);
UnlockResponse unlockResponse1 = client.unlock(unlockRequest1);
assertEquals(unlockResponse1.getStatus(), UnlockResponseStatus.SUCCESS);
logger.info(tryLockResponse1.toString());

TimeUnit.SECONDS.sleep(3);
// unlock key3 failed
UnlockRequest unlockRequest3 = new UnlockRequest(storeName, key3, owner3);
UnlockResponse unlockResponse3 = client.unlock(unlockRequest3);
assertEquals(unlockResponse3.getStatus(), UnlockResponseStatus.LOCK_UNEXIST);
logger.info(unlockResponse3.toString());

}

private static void assertEquals(Object actualResult, Object expected) {
if (actualResult == expected || actualResult.equals(expected)) {
return;
}
String msg = "[Error] Unexpected result:" + actualResult;
System.out.println(msg);
throw new RuntimeException(msg);
}

private static void assertTrue(boolean condition) {
if (condition) {
return;
}
throw new AssertionError();
}
}
18 changes: 18 additions & 0 deletions examples-lock/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n</Property>
</Properties>

<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
59 changes: 0 additions & 59 deletions examples/src/test/java/io/mosn/layotto/examples/lock/Lock.java

This file was deleted.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>examples-pubsub-publisher</module>
<module>examples-preview-service</module>
<module>examples-oss</module>
<module>examples-lock</module>
</modules>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public TryLockResponse(boolean success) {
this.success = success;
}

@Override
public String toString() {
return "TryLockResponse{" +
"success=" + success +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public UnlockResponse(int value) {
public UnlockResponseStatus getStatus() {
return status;
}

@Override
public String toString() {
return "UnlockResponse{" +
"status=" + status +
'}';
}
}

0 comments on commit c4e16f6

Please sign in to comment.