|
| 1 | +@testable import NIORedis |
| 2 | +import XCTest |
| 3 | + |
| 4 | +final class NIORedisPipelineTests: XCTestCase { |
| 5 | + private var redis: NIORedis! |
| 6 | + private var connection: NIORedisConnection! |
| 7 | + |
| 8 | + override func setUp() { |
| 9 | + let redis = NIORedis(executionModel: .spawnThreads(2)) |
| 10 | + |
| 11 | + guard let connection = try? redis.makeConnection().wait() else { |
| 12 | + return XCTFail("Failed to create connection!") |
| 13 | + } |
| 14 | + |
| 15 | + self.redis = redis |
| 16 | + self.connection = connection |
| 17 | + } |
| 18 | + |
| 19 | + override func tearDown() { |
| 20 | + _ = try? connection.command("FLUSHALL").wait() |
| 21 | + connection.close() |
| 22 | + try? redis.terminate() |
| 23 | + } |
| 24 | + |
| 25 | + func test_enqueue() { |
| 26 | + let pipeline = connection.makePipeline() |
| 27 | + |
| 28 | + XCTAssertNoThrow(try pipeline.enqueue(command: "PING")) |
| 29 | + XCTAssertNoThrow(try pipeline.enqueue(command: "SET", arguments: ["KEY", 3])) |
| 30 | + } |
| 31 | + |
| 32 | + func test_executeFails() throws { |
| 33 | + let pipeline = try connection.makePipeline() |
| 34 | + .enqueue(command: "GET") |
| 35 | + |
| 36 | + XCTAssertThrowsError(try pipeline.execute().wait()) |
| 37 | + } |
| 38 | + |
| 39 | + func test_singleCommand() throws { |
| 40 | + let results = try connection.makePipeline() |
| 41 | + .enqueue(command: "PING") |
| 42 | + .execute() |
| 43 | + .wait() |
| 44 | + |
| 45 | + XCTAssertEqual(results[0].string, "PONG") |
| 46 | + } |
| 47 | + |
| 48 | + func test_multipleCommands() throws { |
| 49 | + let results = try connection.makePipeline() |
| 50 | + .enqueue(command: "PING") |
| 51 | + .enqueue(command: "SET", arguments: ["my_key", 3]) |
| 52 | + .enqueue(command: "GET", arguments: ["my_key"]) |
| 53 | + .execute() |
| 54 | + .wait() |
| 55 | + |
| 56 | + XCTAssertEqual(results[0].string, "PONG") |
| 57 | + XCTAssertEqual(results[1].string, "OK") |
| 58 | + XCTAssertEqual(results[2].data, "3".convertedToData()) |
| 59 | + } |
| 60 | + |
| 61 | + func test_executeIsOrdered() throws { |
| 62 | + let results = try connection.makePipeline() |
| 63 | + .enqueue(command: "SET", arguments: ["key", 1]) |
| 64 | + .enqueue(command: "INCR", arguments: ["key"]) |
| 65 | + .enqueue(command: "DECR", arguments: ["key"]) |
| 66 | + .enqueue(command: "INCRBY", arguments: ["key", 15]) |
| 67 | + .execute() |
| 68 | + .wait() |
| 69 | + |
| 70 | + XCTAssertEqual(results[0].string, "OK") |
| 71 | + XCTAssertEqual(results[1].int, 2) |
| 72 | + XCTAssertEqual(results[2].int, 1) |
| 73 | + XCTAssertEqual(results[3].int, 16) |
| 74 | + } |
| 75 | + |
| 76 | + static var allTests = [ |
| 77 | + ("test_enqueue", test_enqueue), |
| 78 | + ("test_executeFails", test_executeFails), |
| 79 | + ("test_singleCommand", test_singleCommand), |
| 80 | + ("test_multipleCommands", test_multipleCommands), |
| 81 | + ("test_executeIsOrdered", test_executeIsOrdered), |
| 82 | + ] |
| 83 | +} |
0 commit comments