Hi,
I noticed unexpected behaviour while testing Ribbon. I have two tests with separate, different contexts. During the tests Ribbon always uses properties from context that started first, even when test is run from the second context.
I have example project with test showing this behaviour: https://github.com/mariuszluciow/ribbon-spring-issue
In more detail, first test:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "client.ribbon.listOfServers=http://localhost:123")
public class ADemoApplicationTests {
@Test
public void contextLoads() {
}
}
and the second one:
@RunWith(SpringRunner.class)
@EnableFeignClients("com.example")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {"server.port=8083", "client.ribbon.listOfServers=http://localhost:${server.port}"})
public class DemoApplication2Tests {
@Autowired
Client client;
@Test
public void contextLoads() {
client.test();
}
@FeignClient("client")
public interface Client {
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
String test();
}
}
When started, Ribbon logs
DynamicServerListLoadBalancer for client client initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=client,current list of Servers=[localhost:123],
...
while it should initialise list of servers with localhost:8083.
Comment From: spencergibb
Use @DirtiesContext
Comment From: mariuszluciow
Hi @spencergibb, thanks for the answer. @DirtiesContext helps, but the question is if this is the right solution, as the first test doesn't suggest any context polluting .
Comment From: spencergibb
We use it fairly regularly.
Comment From: spencergibb
Ribbon has some oddities
Comment From: machowski
@DirtiesContext is often considered to be bad practice - it makes your test suite run way slower - and means that you somehow integrate inside your code under tests, instead of just testing it's behaviour
There are cases when it's useful, but shouldn't be considered as solution to problem reported by mariuszluciow
Comment From: spencergibb
Suggestions welcome. It's what we use in spring cloud.
Comment From: spring-projects-issues
Closing due to age of the question. If you would like us to look at this issue, please comment and we will look at re-opening the issue.
Comment From: k-tomaszewski
@spencergibb @DirtiesContext will not help in this case as the issue is caused by Ribbon which is getting configuration from Archaius which keeps configuration in a global variable (static field). Resetting Spring context will not influence this global state kept by Archaius.
I've discovered a workaround for this and I've described it in a comment for a similar issue: https://github.com/spring-cloud/spring-cloud-netflix/issues/3219#issuecomment-701328327
Maybe we can reopen this ticket?