package org.example
import spock.lang.Specification
class LoadBalancerTest extends Specification {
LoadBalancer loadBalancer = new LoadBalancer()
def "Should retern list of instances when instances was add to load balancer"() {
given:
Instance instance = new Instance("1")
when:
loadBalancer.addInstance(instance)
then:
List<String> instances = loadBalancer.gatAllInstances()
instances.size() == 1
}
def "Should thor Max instances ehen loadBalancer has mor that 10 instances"() {
given:
Instance instance = new Instance("1")
Instance instance1 = new Instance("2")
Instance instance2 = new Instance("3")
Instance instance3 = new Instance("4")
Instance instance4 = new Instance("5")
Instance instance5 = new Instance("6")
Instance instance6 = new Instance("7")
Instance instance7 = new Instance("8")
Instance instance8 = new Instance("9")
Instance instance9 = new Instance("10")
Instance instance10 = new Instance("11")
when:
loadBalancer.addInstance(instance)
loadBalancer.addInstance(instance1)
loadBalancer.addInstance(instance2)
loadBalancer.addInstance(instance3)
loadBalancer.addInstance(instance4)
loadBalancer.addInstance(instance5)
loadBalancer.addInstance(instance6)
loadBalancer.addInstance(instance7)
loadBalancer.addInstance(instance8)
loadBalancer.addInstance(instance9)
loadBalancer.addInstance(instance10)
then:
thrown(RuntimeException)
}
def "Should throw duplicated address when try to add instance with duplicated address"() {
given:
Instance instance = new Instance("1")
Instance instance2 = new Instance("1")
when:
loadBalancer.addInstance(instance)
loadBalancer.addInstance(instance2)
then:
List<String> instances = loadBalancer.gatAllInstances()
instances.size() == 1
}
def "Should return random instance when call get method"() {
given:
Instance instance = new Instance("0")
loadBalancer.addInstance(instance)
when:
Instance receivedInstance = loadBalancer.getRandomInstance()
then:
receivedInstance != null
}
}