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