Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CID-2940 Use 5 max attempts for retrying establishing connection #57

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style:
ReturnCount:
active: false
UnusedPrivateMember:
active: false
active: true
ThrowsCount:
active: false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class WebSocketClientConfig(
private val leanIXProperties: LeanIXProperties,
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties
) {

@Retry(name = "ws-init-session")
@Retry(name = "ws_init_session")
fun initSession(): StompSession {
val headers = WebSocketHttpHeaders()
val stompHeaders = StompHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CachingService(
}

@PostConstruct
@Suppress("UnusedPrivateMember")
private fun init() {
set("baseUrl", gitHubEnterpriseProperties.baseUrl, null)
set("githubAppId", gitHubEnterpriseProperties.gitHubAppId, null)
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ leanix:
technical-user-token: ${LEANIX_TECHNICAL_USER_TOKEN}
webhookEventService:
waitingTime: 10000

resilience4j.retry:
configs:
default:
maxAttempts: 5
enable-exponential-backoff: true
exponential-backoff-multiplier: 2
waitDuration: 1000
henriq-amaral-leanix marked this conversation as resolved.
Show resolved Hide resolved

Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
package net.leanix.githubagent.config

// class WebSocketClientConfigTests {
//
// private lateinit var webSocketClientConfig: WebSocketClientConfig
// private val authService: AuthService = mockk()
// private val stompClient: WebSocketStompClient = mockk(relaxed = true)
// private val stompSession: StompSession = mockk(relaxed = true)
// private val brokerStompSessionHandler: BrokerStompSessionHandler = mockk(relaxed = true)
// private val objectMapper: ObjectMapper = mockk(relaxed = true)
// private val leanIXProperties: LeanIXProperties = mockk(relaxed = true)
// private val gitHubEnterpriseProperties: GitHubEnterpriseProperties = mockk(relaxed = true)
//
// @BeforeEach
// fun setUp() {
// webSocketClientConfig = WebSocketClientConfig(
// brokerStompSessionHandler,
// objectMapper,
// authService,
// leanIXProperties,
// gitHubEnterpriseProperties
// )
// webSocketClientConfig.stompClient = stompClient
// }
//
// @Test
// fun `initSession should establish connection with valid headers`() = runBlocking {
// coEvery { authService.getBearerToken() } returns "validToken"
// coEvery { leanIXProperties.wsBaseUrl } returns "ws://test.url"
// coEvery { gitHubEnterpriseProperties.baseUrl } returns "http://github.enterprise.url"
// coEvery { gitHubEnterpriseProperties.gitHubAppId } returns "appId"
// coEvery { stompClient.connect(any(), any(), any(), any()) } returns stompSession
//
// val session = webSocketClientConfig.initSession()
//
// assertEquals(stompSession, session)
// }
//
// @Test
// fun `initSession should handle connection failure gracefully`() = runBlocking {
// coEvery { authService.getBearerToken() } returns "validToken"
// coEvery { leanIXProperties.wsBaseUrl } returns "ws://test.url"
// coEvery { gitHubEnterpriseProperties.baseUrl } returns "http://github.enterprise.url"
// coEvery { gitHubEnterpriseProperties.gitHubAppId } returns "appId"
// coEvery { stompClient.connect(any(), any(), any(), any()) } throws RuntimeException("Connection failed")
//
// val session = runCatching { webSocketClientConfig.initSession() }.getOrNull()
//
// assertEquals(null, session)
// }
// }
import com.fasterxml.jackson.databind.ObjectMapper
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.runBlocking
import net.leanix.githubagent.handler.BrokerStompSessionHandler
import net.leanix.githubagent.services.LeanIXAuthService
import net.leanix.githubagent.shared.GitHubAgentProperties
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.messaging.simp.stomp.StompHeaders
import org.springframework.messaging.simp.stomp.StompSession
import org.springframework.web.socket.WebSocketHttpHeaders
import org.springframework.web.socket.messaging.WebSocketStompClient

class WebSocketClientConfigTests {
private lateinit var webSocketClientConfig: WebSocketClientConfig
private lateinit var stompClient: WebSocketStompClient
private lateinit var stompSession: StompSession
private lateinit var authService: LeanIXAuthService
private lateinit var leanIXProperties: LeanIXProperties
private lateinit var gitHubEnterpriseProperties: GitHubEnterpriseProperties
private lateinit var leanIXAuthService: LeanIXAuthService

@BeforeEach
fun setUp() {
val brokerStompSessionHandler = mockk<BrokerStompSessionHandler>()
val objectMapper = mockk<ObjectMapper>()
leanIXProperties = mockk()
gitHubEnterpriseProperties = mockk()
stompClient = mockk()
stompSession = mockk()
authService = mockk()
leanIXAuthService = mockk()
webSocketClientConfig = WebSocketClientConfig(
brokerStompSessionHandler,
objectMapper,
leanIXAuthService,
leanIXProperties,
gitHubEnterpriseProperties
)

GitHubAgentProperties.GITHUB_AGENT_VERSION = "test-version"
}

@Test
fun `initSession should fail after max retry attempts`() = runBlocking {
coEvery { authService.getBearerToken() } returns "validToken"
coEvery { leanIXProperties.wsBaseUrl } returns "ws://test.url"
coEvery { gitHubEnterpriseProperties.baseUrl } returns "http://github.enterprise.url"
coEvery { gitHubEnterpriseProperties.gitHubAppId } returns "appId"
coEvery { leanIXAuthService.getBearerToken() } returns "bearer token"
coEvery {
stompClient.connectAsync(
any<String>(), any<WebSocketHttpHeaders>(),
any<StompHeaders>(), any<BrokerStompSessionHandler>()
)
} throws RuntimeException("Connection failed")

val session = runCatching { webSocketClientConfig.initSession() }.getOrNull()

assertEquals(null, session)
}
}
Loading