Skip to content

Commit dbb0d0d

Browse files
committed
Make Kotlin tests more robust
1 parent 31dcea5 commit dbb0d0d

File tree

1 file changed

+50
-13
lines changed
  • bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode

1 file changed

+50
-13
lines changed

bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@ fun runCommandAndWait(cmd: String): String {
2323
return stdout + stderr
2424
}
2525

26-
fun mine(blocks: UInt) {
26+
fun mine(blocks: UInt): String {
2727
val address = runCommandAndWait("bitcoin-cli -regtest getnewaddress")
2828
val output = runCommandAndWait("bitcoin-cli -regtest generatetoaddress $blocks $address")
2929
println("Mining output: $output")
30+
val re = Regex("\n.+\n\\]$")
31+
val lastBlock = re.find(output)!!.value.replace("]","").replace("\"", "").replace("\n","").trim()
32+
println("Last block: $lastBlock")
33+
return lastBlock
34+
}
35+
36+
fun mineAndWait(esploraEndpoint: String, blocks: UInt) {
37+
val lastBlockHash = mine(blocks)
38+
waitForBlock(esploraEndpoint, lastBlockHash)
3039
}
3140

3241
fun sendToAddress(address: String, amountSats: UInt): String {
@@ -39,6 +48,7 @@ fun setup() {
3948
runCommandAndWait("bitcoin-cli -regtest createwallet ldk_node_test")
4049
runCommandAndWait("bitcoin-cli -regtest loadwallet ldk_node_test true")
4150
mine(101u)
51+
Thread.sleep(5_000)
4252
}
4353

4454
fun waitForTx(esploraEndpoint: String, txid: String) {
@@ -53,16 +63,32 @@ fun waitForTx(esploraEndpoint: String, txid: String) {
5363
val response = client.send(request, HttpResponse.BodyHandlers.ofString());
5464

5565
esploraPickedUpTx = re.containsMatchIn(response.body());
56-
Thread.sleep(1_000)
66+
Thread.sleep(500)
67+
}
68+
}
69+
70+
fun waitForBlock(esploraEndpoint: String, blockHash: String) {
71+
var esploraPickedUpBlock = false
72+
val re = Regex("\"in_best_chain\":true");
73+
while (!esploraPickedUpBlock) {
74+
val client = HttpClient.newBuilder().build()
75+
val request = HttpRequest.newBuilder()
76+
.uri(URI.create(esploraEndpoint + "/block/" + blockHash + "/status"))
77+
.build();
78+
79+
val response = client.send(request, HttpResponse.BodyHandlers.ofString());
80+
val body = response.body()
81+
82+
esploraPickedUpBlock = re.containsMatchIn(response.body());
83+
Thread.sleep(500)
5784
}
5885
}
5986

6087
class LibraryTest {
6188
@Test fun fullCycle() {
89+
val esploraEndpoint = "http://127.0.0.1:3002"
6290
setup()
6391

64-
val network = Network.REGTEST
65-
6692
val tmpDir1 = createTempDirectory("ldk_node").toString()
6793
println("Random dir 1: $tmpDir1")
6894
val tmpDir2 = createTempDirectory("ldk_node").toString()
@@ -71,13 +97,27 @@ class LibraryTest {
7197
val listenAddress1 = "127.0.0.1:2323"
7298
val listenAddress2 = "127.0.0.1:2324"
7399

74-
val esploraEndpoint = "http://127.0.0.1:3002"
100+
val logLevel = LogLevel.TRACE;
101+
102+
val config1 = Config()
103+
config1.storageDirPath = tmpDir1
104+
config1.listeningAddress = listenAddress1
105+
config1.network = Network.REGTEST
106+
config1.logLevel = LogLevel.TRACE
75107

76-
val config1 = Config(tmpDir1, esploraEndpoint, network, listenAddress1, 2048u)
77-
val config2 = Config(tmpDir2, esploraEndpoint, network, listenAddress2, 2048u)
108+
println("Config 1: $config1")
109+
110+
val config2 = Config()
111+
config2.storageDirPath = tmpDir2
112+
config2.listeningAddress = listenAddress2
113+
config2.network = Network.REGTEST
114+
config2.logLevel = LogLevel.TRACE
115+
println("Config 2: $config2")
78116

79117
val builder1 = Builder.fromConfig(config1)
118+
builder1.setEsploraServer(esploraEndpoint)
80119
val builder2 = Builder.fromConfig(config2)
120+
builder2.setEsploraServer(esploraEndpoint)
81121

82122
val node1 = builder1.build()
83123
val node2 = builder2.build()
@@ -99,7 +139,7 @@ class LibraryTest {
99139

100140
val txid1 = sendToAddress(address1, 100000u)
101141
val txid2 = sendToAddress(address2, 100000u)
102-
mine(6u)
142+
mineAndWait(esploraEndpoint, 6u)
103143

104144
waitForTx(esploraEndpoint, txid1)
105145
waitForTx(esploraEndpoint, txid2)
@@ -139,7 +179,7 @@ class LibraryTest {
139179

140180
waitForTx(esploraEndpoint, fundingTxid)
141181

142-
mine(6u)
182+
mineAndWait(esploraEndpoint, 6u)
143183

144184
node1.syncWallets()
145185
node2.syncWallets()
@@ -196,10 +236,7 @@ class LibraryTest {
196236
assert(channelClosedEvent2 is Event.ChannelClosed)
197237
node2.eventHandled()
198238

199-
mine(1u)
200-
201-
// Sleep a bit to allow for the block to propagate to esplora
202-
Thread.sleep(5_000)
239+
mineAndWait(esploraEndpoint, 1u)
203240

204241
node1.syncWallets()
205242
node2.syncWallets()

0 commit comments

Comments
 (0)