테스트 대상 시스템 로그인시 세션이 test() 이후에 저장되는 현상 #839
-
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Date
import java.util.List
import java.util.ArrayList
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
/**
* A simple example using the HTTP plugin that shows the retrieval of a
* single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = [
new NVPair("Content-Type", "application/json")
]
public static NVPair[] params = []
public static Cookie[] cookies = []
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000000
test = new GTest(1, "192.168.219.118")
request = new HTTPRequest();
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
test.record(this, "test")
String body = """{"memberId": "test", "memberPassword": "test11"}""";
HTTPResponse result = request.POST("http://192.168.219.118:8081/apis/auth/signIn", body.getBytes(), headers);
grinder.statistics.delayReports=true;
assertThat(result.statusCode, is(200));
grinder.logger.info("before thread.");
}
@Before
public void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
}
@Test
public void test(){
HTTPResponse result = request.POST("http://192.168.219.118:8081/apis/posts", params)
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
} else {
assertThat(result.statusCode, is(200));
}
}
} 위와 같이, beforeThread()에서 테스트 대상이 되는 시스템에 로그인 처리를 하고 test()에서 세션을 실어 스트레스 테스트를 진행하려 합니다. 그런데 제 서버 시스템 상에서는 beforeThread() 요청과 test() 요청을 하나씩 처리하는 것이 아니라 한 요청처럼 처리하여서 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
@imbyungjun plz. check it |
Beta Was this translation helpful? Give feedback.
-
로컬에서 실행중인 서버가 어떻게 동작하는지 정확하게는 알 수 없지만, 가장 먼저 의심되는 부분은 cookie 처리에 대한 부분입니다. 이슈 해결을 위해서 Groovy 로그인 샘플 코드를 참고하여 로그인 요청 이후 쿠키를 추가해보는게 좋을 것 같습니다. 예시) @BeforeThread
public void beforeThread() {
HTTPResponse result = request.POST("http://192.168.219.118:8081/apis/auth/signIn", body.getBytes(), headers);
def threadContext = HTTPPluginControl.getThreadHTTPClientContext()
cookies = CookieModule.listAllCookies(threadContext)
}
@Before
public void before() {
def threadContext = HTTPPluginControl.getThreadHTTPClientContext()
cookies.each { CookieModule.addCookie(it ,threadContext) }
} |
Beta Was this translation helpful? Give feedback.
-
@imbyungjun 해당 코드를 삽입하니 세션이 정상적으로 전송되어 문제가 해결되었습니다. |
Beta Was this translation helpful? Give feedback.
로컬에서 실행중인 서버가 어떻게 동작하는지 정확하게는 알 수 없지만, 가장 먼저 의심되는 부분은 cookie 처리에 대한 부분입니다.
일반적인 경우 로그인 후 세션 ID를 쿠키에 저장하게 될 텐데요. 올려주신 테스트 스크립트에서는 쿠키에 대한 처리를 수행하지 않고 있습니다. 위 스크립트에서 사용하는 HTTPRequest 클래스는 상당히 낮은 추상화 수준의 구현체로 쿠키에 대한 처리도 직접 수행해주어야합니다.
이슈 해결을 위해서 Groovy 로그인 샘플 코드를 참고하여 로그인 요청 이후 쿠키를 추가해보는게 좋을 것 같습니다.
예시)