|
1 | 1 | #if ClientNIO |
2 | 2 | import AsyncHTTPClient |
| 3 | +import Foundation |
3 | 4 | import Haystack |
4 | 5 | import HaystackClient |
5 | 6 | import NIO |
6 | | -import XCTest |
| 7 | +import Testing |
7 | 8 |
|
8 | 9 | /// To use these tests, run a [Haxall](https://github.com/haxall/haxall) server and set the username and password |
9 | 10 | /// in the `HAYSTACK_USER` and `HAYSTACK_PASSWORD` environment variables |
10 | | -final class HaystackClientNIOIntegrationTests: XCTestCase { |
11 | | - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
12 | | - var httpClient: HTTPClient! |
13 | | - var client: Client! |
14 | | - |
15 | | - override func setUp() async throws { |
16 | | - httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup)) |
17 | | - client = try Client( |
| 11 | +struct HaystackClientNIOIntegrationTests { |
| 12 | + private func withClient(_ block: (Client) async throws -> Void) async throws { |
| 13 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 14 | + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup)) |
| 15 | + let client = try Client( |
18 | 16 | baseUrl: "http://localhost:8080/api/", |
19 | 17 | username: ProcessInfo.processInfo.environment["HAYSTACK_USER"] ?? "su", |
20 | 18 | password: ProcessInfo.processInfo.environment["HAYSTACK_PASSWORD"] ?? "su", |
21 | 19 | httpClient: httpClient |
22 | 20 | ) |
23 | 21 | try await client.open() |
24 | | - } |
25 | 22 |
|
26 | | - override func tearDown() async throws { |
| 23 | + try await block(client) |
| 24 | + |
27 | 25 | try await client.close() |
28 | 26 | try await httpClient.shutdown() |
29 | 27 | } |
30 | 28 |
|
31 | | - func testCloseAndOpen() async throws { |
32 | | - try print(await client.close()) |
33 | | - try print(await client.open()) |
| 29 | + @Test func closeAndOpen() async throws { |
| 30 | + try await withClient { client in |
| 31 | + try print(await client.close()) |
| 32 | + try print(await client.open()) |
| 33 | + } |
34 | 34 | } |
35 | 35 |
|
36 | | - func testAbout() async throws { |
37 | | - try print(await client.about().toZinc()) |
| 36 | + @Test func about() async throws { |
| 37 | + try await withClient { client in |
| 38 | + try print(await client.about().toZinc()) |
| 39 | + } |
38 | 40 | } |
39 | 41 |
|
40 | | - func testDefs() async throws { |
41 | | - try print(await client.defs().toZinc()) |
42 | | - try print(await client.defs(filter: "lib==^lib:phIoT").toZinc()) |
43 | | - try print(await client.defs(limit: Number(1)).toZinc()) |
44 | | - try print(await client.defs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 42 | + @Test func defs() async throws { |
| 43 | + try await withClient { client in |
| 44 | + try print(await client.defs().toZinc()) |
| 45 | + try print(await client.defs(filter: "lib==^lib:phIoT").toZinc()) |
| 46 | + try print(await client.defs(limit: Number(1)).toZinc()) |
| 47 | + try print(await client.defs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 48 | + } |
45 | 49 | } |
46 | 50 |
|
47 | | - func testLibs() async throws { |
48 | | - try print(await client.libs().toZinc()) |
49 | | - try print(await client.libs(filter: "lib==^lib:phIoT").toZinc()) |
50 | | - try print(await client.libs(limit: Number(1)).toZinc()) |
51 | | - try print(await client.libs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 51 | + @Test func libs() async throws { |
| 52 | + try await withClient { client in |
| 53 | + try print(await client.libs().toZinc()) |
| 54 | + try print(await client.libs(filter: "lib==^lib:phIoT").toZinc()) |
| 55 | + try print(await client.libs(limit: Number(1)).toZinc()) |
| 56 | + try print(await client.libs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 57 | + } |
52 | 58 | } |
53 | 59 |
|
54 | | - func testOps() async throws { |
55 | | - try print(await client.ops().toZinc()) |
56 | | - try print(await client.ops(filter: "lib==^lib:phIoT").toZinc()) |
57 | | - try print(await client.ops(limit: Number(1)).toZinc()) |
58 | | - try print(await client.ops(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 60 | + @Test func ops() async throws { |
| 61 | + try await withClient { client in |
| 62 | + try print(await client.ops().toZinc()) |
| 63 | + try print(await client.ops(filter: "lib==^lib:phIoT").toZinc()) |
| 64 | + try print(await client.ops(limit: Number(1)).toZinc()) |
| 65 | + try print(await client.ops(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 66 | + } |
59 | 67 | } |
60 | 68 |
|
61 | | - func testFiletypes() async throws { |
62 | | - try print(await client.filetypes().toZinc()) |
63 | | - try print(await client.filetypes(filter: "lib==^lib:phIoT").toZinc()) |
64 | | - try print(await client.filetypes(limit: Number(1)).toZinc()) |
65 | | - try print(await client.filetypes(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 69 | + @Test func filetypes() async throws { |
| 70 | + try await withClient { client in |
| 71 | + try print(await client.filetypes().toZinc()) |
| 72 | + try print(await client.filetypes(filter: "lib==^lib:phIoT").toZinc()) |
| 73 | + try print(await client.filetypes(limit: Number(1)).toZinc()) |
| 74 | + try print(await client.filetypes(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 75 | + } |
66 | 76 | } |
67 | 77 |
|
68 | | - func testRead() async throws { |
69 | | - try print(await client.read(ids: [Ref("28e7fb47-d67ab19a")]).toZinc()) |
| 78 | + @Test func read() async throws { |
| 79 | + try await withClient { client in |
| 80 | + try print(await client.read(ids: [Ref("28e7fb47-d67ab19a")]).toZinc()) |
| 81 | + } |
70 | 82 | } |
71 | 83 |
|
72 | | - func testReadAll() async throws { |
73 | | - try print(await client.read(filter: "site").toZinc()) |
| 84 | + @Test func readAll() async throws { |
| 85 | + try await withClient { client in |
| 86 | + try print(await client.read(filter: "site").toZinc()) |
| 87 | + } |
74 | 88 | } |
75 | 89 |
|
76 | | - func testHisRead() async throws { |
77 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .today).toZinc()) |
78 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday).toZinc()) |
79 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .date(Date("2022-01-01"))).toZinc()) |
80 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateRange(from: Date("2022-01-01"), to: Date("2022-02-01"))).toZinc()) |
81 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateTimeRange(from: DateTime("2022-01-01T00:00:00Z"), to: DateTime("2022-02-01T00:00:00Z"))).toZinc()) |
82 | | - try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .after(DateTime("2022-01-01T00:00:00Z"))).toZinc()) |
| 90 | + @Test func hisRead() async throws { |
| 91 | + try await withClient { client in |
| 92 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .today).toZinc()) |
| 93 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday).toZinc()) |
| 94 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .date(Date("2022-01-01"))).toZinc()) |
| 95 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateRange(from: Date("2022-01-01"), to: Date("2022-02-01"))).toZinc()) |
| 96 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateTimeRange(from: DateTime("2022-01-01T00:00:00Z"), to: DateTime("2022-02-01T00:00:00Z"))).toZinc()) |
| 97 | + try print(await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .after(DateTime("2022-01-01T00:00:00Z"))).toZinc()) |
| 98 | + } |
83 | 99 | } |
84 | 100 |
|
85 | | - func testHisWrite() async throws { |
86 | | - try print(await client.hisWrite( |
87 | | - id: Ref("28e7fb7d-e20316e0"), |
88 | | - items: [ |
89 | | - HisItem(ts: DateTime("2022-01-01T00:00:00-07:00 Denver"), val: Number(14)), |
90 | | - ] |
91 | | - ).toZinc()) |
| 101 | + @Test func hisWrite() async throws { |
| 102 | + try await withClient { client in |
| 103 | + try print(await client.hisWrite( |
| 104 | + id: Ref("28e7fb7d-e20316e0"), |
| 105 | + items: [ |
| 106 | + HisItem(ts: DateTime("2022-01-01T00:00:00-07:00 Denver"), val: Number(14)), |
| 107 | + ] |
| 108 | + ).toZinc()) |
| 109 | + } |
92 | 110 | } |
93 | 111 |
|
94 | | - func testEval() async throws { |
95 | | - try print(await client.eval(expression: "readAll(site)").toZinc()) |
| 112 | + @Test func eval() async throws { |
| 113 | + try await withClient { client in |
| 114 | + try print(await client.eval(expression: "readAll(site)").toZinc()) |
| 115 | + } |
96 | 116 | } |
97 | 117 |
|
98 | | - func testWatchUnsub() async throws { |
99 | | - try print(await client.watchUnsubRemove(watchId: "id", ids: [Ref("28e7fb47-d67ab19a")])) |
| 118 | + @Test func watchUnsub() async throws { |
| 119 | + try await withClient { client in |
| 120 | + try print(await client.watchUnsubRemove(watchId: "id", ids: [Ref("28e7fb47-d67ab19a")])) |
| 121 | + } |
100 | 122 | } |
101 | 123 | } |
102 | 124 | #endif |
0 commit comments