-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.yen</groupId> | ||
<artifactId>MyApp</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- <dependency>--> | ||
<!-- <groupId>junit</groupId>--> | ||
<!-- <artifactId>junit</artifactId>--> | ||
<!-- <version>4.13.2</version>--> | ||
<!-- <scope>test</scope>--> | ||
<!-- </dependency>--> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core --> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>5.7.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>RELEASE</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.yen; | ||
|
||
/** | ||
* LB | ||
* | ||
* 1. registry instance | ||
* 2. channel selected | ||
* 3. address unique (can't register same again) | ||
* | ||
* - It should be possible to register an instance, | ||
* | ||
* - identified by an address | ||
* | ||
* - Each address should be unique, | ||
* | ||
* - it should not be possible to register the same address more than once | ||
* | ||
* - Load Balancer should accept up to 10 addresses (in total) | ||
* | ||
* - consider concurrency | ||
* | ||
* - validate input address | ||
* | ||
* no db, in memory | ||
* no http | ||
* | ||
* high level : | ||
* | ||
* server A -> LB (save) | ||
* <-- id | ||
* | ||
* server A -> LB | ||
* <-- | ||
* | ||
* | ||
* | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello world!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.yen.bean; | ||
|
||
public class LBMapping { | ||
|
||
private String address; | ||
private String id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.yen.bean; | ||
|
||
public class Server { | ||
|
||
private String address; // TODO : validate incoming address | ||
} |
10 changes: 10 additions & 0 deletions
10
dev_projects/MyApp/src/main/java/com/yen/service/RegisterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.yen.service; | ||
|
||
public interface RegisterService { | ||
|
||
Boolean isExisted(String address); | ||
Boolean register(String address); | ||
|
||
Boolean validate(String address); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
dev_projects/MyApp/src/main/java/com/yen/service/impl/RegisterServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.yen.service.impl; | ||
|
||
import com.yen.service.RegisterService; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class RegisterServiceImpl implements RegisterService { | ||
|
||
//private HashMap<String, Object> map; | ||
private ConcurrentHashMap<String, Object> map; | ||
|
||
// ConcurrentHashMap | ||
|
||
public RegisterServiceImpl(){ | ||
|
||
map = new ConcurrentHashMap(); | ||
} | ||
|
||
public RegisterServiceImpl(ConcurrentHashMap<String, Object> map){ | ||
this.map = map; | ||
} | ||
|
||
@Override | ||
public Boolean isExisted(String address) { | ||
return map.containsKey(address); | ||
} | ||
|
||
@Override | ||
public Boolean register(String address) { | ||
|
||
if(map.keySet().size() >= 10){ | ||
System.out.println("Already saved 10 Keys"); | ||
return false; | ||
} | ||
|
||
if (map.containsKey(address)){ | ||
System.out.println("Key already existed"); | ||
return false; | ||
} | ||
|
||
String randomId = UUID.randomUUID().toString(); | ||
map.put(address, randomId); | ||
return true; | ||
} | ||
|
||
@Override | ||
public Boolean validate(String address) { | ||
|
||
// 127.0.0.1 | ||
// check if only numeric digit | ||
//if () | ||
|
||
return null; | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
dev_projects/MyApp/src/test/java/com/yen/service/impl/RegisterServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.yen.service.impl; | ||
|
||
//import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.yen.service.RegisterService; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
//@RunWith(MockitoExten) | ||
//@ExtendWith(Mockito.class) | ||
class RegisterServiceImplTest { | ||
|
||
private ConcurrentHashMap<String, Object> map; | ||
|
||
private RegisterServiceImpl registerService; | ||
|
||
@BeforeEach | ||
public void setup(){ | ||
|
||
map = new ConcurrentHashMap<>(); | ||
map.put("address-a", "key-a"); | ||
|
||
registerService = new RegisterServiceImpl(map); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTrueIfExist(){ | ||
|
||
String myAddress = "address-a"; | ||
Assertions.assertEquals(registerService.isExisted(myAddress), true); | ||
} | ||
|
||
@Test | ||
public void shouldReturnFalseIfNotExist(){ | ||
|
||
String myAddress = "address-b"; | ||
Assertions.assertEquals(registerService.isExisted(myAddress), false); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTrueIfAddressNotInMemory(){ | ||
|
||
String myAddress = "address-c"; | ||
Assertions.assertEquals(registerService.register(myAddress), true); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldReturnFalseIfAddressInMemory(){ | ||
|
||
String myAddress = "address-a"; | ||
Assertions.assertEquals(registerService.register(myAddress), false); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldReturnFalseIfSaveOverTenAddress(){ | ||
|
||
for (int i = 0; i < 10; i++){ | ||
map.put(String.valueOf(i), UUID.randomUUID().toString()); | ||
} | ||
|
||
String myAddress = "address-a"; | ||
System.out.println(map.keySet().size()); | ||
Assertions.assertEquals(registerService.register(myAddress), false); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTrueIfNormalAddress(){ | ||
|
||
//String myAddress = "dev.com/..."; | ||
String myAddress = "127.0.0.1"; | ||
Assertions.assertEquals(registerService.validate(myAddress), true); | ||
} | ||
|
||
|
||
|
||
} |