2023-03-08 15:04:54 +00:00
|
|
|
import std/os
|
|
|
|
import std/httpclient
|
2023-11-09 08:47:09 +00:00
|
|
|
import std/strutils
|
2023-08-01 23:47:57 +00:00
|
|
|
from std/net import TimeoutError
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import ../ethertest
|
|
|
|
import ./nodes
|
|
|
|
|
|
|
|
ethersuite "Node block expiration tests":
|
|
|
|
var node: NodeProcess
|
|
|
|
var baseurl: string
|
|
|
|
|
|
|
|
let dataDir = getTempDir() / "Codex1"
|
|
|
|
let content = "test file content"
|
|
|
|
|
|
|
|
setup:
|
|
|
|
baseurl = "http://localhost:8080/api/codex/v1"
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
node.stop()
|
|
|
|
|
|
|
|
dataDir.removeDir()
|
|
|
|
|
|
|
|
proc startTestNode(blockTtlSeconds: int) =
|
|
|
|
node = startNode([
|
|
|
|
"--api-port=8080",
|
|
|
|
"--data-dir=" & dataDir,
|
|
|
|
"--nat=127.0.0.1",
|
2023-10-24 14:52:06 +00:00
|
|
|
"--listen-addrs=/ip4/127.0.0.1/tcp/0",
|
2023-03-08 15:04:54 +00:00
|
|
|
"--disc-ip=127.0.0.1",
|
|
|
|
"--disc-port=8090",
|
|
|
|
"--block-ttl=" & $blockTtlSeconds,
|
2023-03-27 13:47:25 +00:00
|
|
|
"--block-mi=1",
|
2023-03-08 15:04:54 +00:00
|
|
|
"--block-mn=10"
|
|
|
|
], debug = false)
|
2023-09-13 14:17:56 +00:00
|
|
|
node.waitUntilStarted()
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
proc uploadTestFile(): string =
|
|
|
|
let client = newHttpClient()
|
2023-11-09 08:47:09 +00:00
|
|
|
let uploadUrl = baseurl & "/data"
|
2023-03-08 15:04:54 +00:00
|
|
|
let uploadResponse = client.post(uploadUrl, content)
|
|
|
|
check uploadResponse.status == "200 OK"
|
|
|
|
client.close()
|
|
|
|
uploadResponse.body
|
|
|
|
|
|
|
|
proc downloadTestFile(contentId: string): Response =
|
|
|
|
let client = newHttpClient(timeout=3000)
|
2023-11-09 08:47:09 +00:00
|
|
|
let downloadUrl = baseurl & "/data/" & contentId
|
2023-03-08 15:04:54 +00:00
|
|
|
let content = client.get(downloadUrl)
|
|
|
|
client.close()
|
|
|
|
content
|
|
|
|
|
2023-11-09 08:47:09 +00:00
|
|
|
proc hasFile(contentId: string): bool =
|
|
|
|
let client = newHttpClient(timeout=3000)
|
|
|
|
let dataLocalUrl = baseurl & "/local"
|
|
|
|
let content = client.get(dataLocalUrl)
|
|
|
|
client.close()
|
|
|
|
return content.body.contains(contentId)
|
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
test "node retains not-expired file":
|
2023-03-27 13:47:25 +00:00
|
|
|
startTestNode(blockTtlSeconds = 10)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
let contentId = uploadTestFile()
|
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
await sleepAsync(2.seconds)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
let response = downloadTestFile(contentId)
|
|
|
|
check:
|
2023-11-09 08:47:09 +00:00
|
|
|
hasFile(contentId)
|
2023-03-08 15:04:54 +00:00
|
|
|
response.status == "200 OK"
|
|
|
|
response.body == content
|
|
|
|
|
|
|
|
test "node deletes expired file":
|
2023-03-27 13:47:25 +00:00
|
|
|
startTestNode(blockTtlSeconds = 1)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
let contentId = uploadTestFile()
|
|
|
|
|
2023-09-04 09:12:14 +00:00
|
|
|
await sleepAsync(3.seconds)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
2023-11-09 08:47:09 +00:00
|
|
|
check:
|
|
|
|
not hasFile(contentId)
|
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
expect TimeoutError:
|
|
|
|
discard downloadTestFile(contentId)
|