顯示具有 Groovy 標籤的文章。 顯示所有文章
顯示具有 Groovy 標籤的文章。 顯示所有文章

2020年3月18日 星期三

在 Spock Framework 測試包含 parallel stream 的標的時卡住

紀錄一下最近解了幾天的問題,程式碼大體上類似 [1] 裡面的 MyRecordProcessor 和 MyRecordProcessorTest 那樣。因為本來是要建置環境比較簡單的測試來重現問題,而原本的問題是發生在開發 Kinesis 的 RecordProcessor 中遇到的,因此 [1] 的範例也沿用了這個關係。不過實際上最後發現的原因跟 Kinesis 沒有關係就是了。

問題的徵兆是,當要測試的標的內部改寫成有 parallel 的狀況時,Spock 這邊執行測試就可能會出現測試整個卡在某個地方的狀況。卡住時會完全沒有回應,但也沒有拋出錯誤或者停止執行。這個問題好像遇到的人並不是那麼多,所以可以找到的資料也非常少。不過主要參考了 [2-3],最後確認的原因是因為測試時用了 Spy。Spy 好像存在某些限制(在 [3] 的回答中有提到,雖然他沒提到到底是什麼限制),實際上似乎導致在 parallel 的狀況時,Spy 的 dynamic proxy 似乎有時會生效、但有時會無效。所以最後解決方法就是~不要用 Spy。

參考資料
  1. spock-parallel-test
  2. Spock unit test is stuck when parallel stream is used
  3. Using thread pools in a Spock Spy'd class hangs during unit tests

2019年7月14日 星期日

Groovy 的 RESTClient 相關

忽略 HTTPS 的伺服器端檢查

restClient.ignoreSSLIssues()
回覆的 status code > 399 時,不要拋出 exception REST

Client restClient = new RESTClient()
restClient.handler.failure = restClient.handler.success
參考資料
  1. Is there an easier way to tell HTTPBuilder to ignore an invalid cert?
  2. Can't use ignoreSSLIssues in HttpBuilder version 0.7.1
  3. Can I override RESTClient default “HttpResponseException” response to >399 Return Codes?

2019年7月3日 星期三

在 Maven 專案中自行指定原始碼的位置

在我們團隊的習慣,會在 Maven 專案裡面使用 Groovy 來寫測試,並且在寫測試時會用 Cucumber 來做整合測試。其中整合測試也許是為了想要跟單元測試分開,所以不會擺在一般 Maven 預設的路徑 src/test 裡面,而是會放在 src/integration-test 裡面,這樣的狀況會導致 IDE 無法正確找到原始碼,Maven 的 dependency 設定也會出現問題。

舉例來說,在我的案例裡,因為 Cucumber 只有測試階段才會使用,因此很自然我會在 Maven 上做這樣的 dependency 設定:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>4.3.1</version>
    <scope>test</scope>
</dependency>

這邊的 scope 會特地設定成 test,表示 Cucumber 的套件只有在測試期間才會被載入。接著我如果直接上把 src/integration-test/groovy 加入 build path,會因為 Cucumber 只會在測試階段被引用的關係,導致 IDE 發出編譯錯誤的訊息。根本來說,問題是在於我必須要好好地告訴 IDE 或者 Maven 說,src/integration-test/groovy 這個資料夾應該要被視為是測試的資料夾。

所幸,Maven 本身有方法可以自行指定資料夾的功能,也就是利用 build-helper-maven-plugin 這個 plugin。這個 plugin 的功能主要就是讓我們可以告訴 Maven 說哪個資料夾要看成是 source directory、哪個資料夾要看成是 test directory 等等。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${maven-build-helper-plugin.version}</version>
            <executions>
                <execution>
                    <id>add-test-sources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                            <source>src/test/java</source>
                            <source>src/integration-test/groovy</source>
                            <source>src/integration-test/java</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-resources</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>add-test-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/integration-test/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

可以注意到,在上述的 POM 當中,我設定了 src/integration-test/javasrc/integration-test/groovy 兩個資料夾都是 test-source 的 source,而 src/integration-test/resources 則是 test-resource 的 resource。如此一來,我就可以在 eclipse 裡面直接依靠 Maven 來幫我設定專案的原始碼資料夾了。

參考資料
  1. build-helper-maven-plugin
  2. Maven插件之build-helper-maven-plugin