Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 26, 2023
1 parent b6c70ea commit 89deccf
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 0 deletions.
49 changes: 49 additions & 0 deletions dev_projects/MyApp/pom.xml
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>
42 changes: 42 additions & 0 deletions dev_projects/MyApp/src/main/java/com/yen/Main.java
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!");
}
}
7 changes: 7 additions & 0 deletions dev_projects/MyApp/src/main/java/com/yen/bean/LBMapping.java
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;
}
6 changes: 6 additions & 0 deletions dev_projects/MyApp/src/main/java/com/yen/bean/Server.java
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
}
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);

}
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;
}

}
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);
}



}

0 comments on commit 89deccf

Please sign in to comment.