Skip to content

Commit 17f7736

Browse files
committed
Fix scan method and add unit test
1 parent d7b029f commit 17f7736

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Sources/NIORedis/Commands/BasicCommands.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,29 +195,33 @@ extension RedisCommandExecutor {
195195
///
196196
/// [https://redis.io/commands/scan](https://redis.io/commands/scan)
197197
/// - Parameters:
198+
/// - startingFrom: The cursor position to start from.
198199
/// - count: The number of elements to advance by. Redis default is 10.
199200
/// - matching: A glob-style pattern to filter values to be selected from the result set.
200201
/// - Returns: A cursor position for additional invocations with a limited collection of keys stored in the database.
201202
@inlinable
202203
public func scan(
203-
_ key: String,
204-
atPosition pos: Int = 0,
204+
startingFrom pos: Int = 0,
205205
count: Int? = nil,
206206
matching match: String? = nil) -> EventLoopFuture<(Int, [String])>
207207
{
208-
return _scan(command: "SCAN", resultType: [String].self, key, pos, count, match)
208+
return _scan(command: "SCAN", resultType: [String].self, nil, pos, count, match)
209209
}
210210

211211
@inline(__always)
212212
@usableFromInline func _scan<T: RESPValueConvertible>(
213213
command: String,
214214
resultType: T.Type,
215-
_ key: String,
215+
_ key: String?,
216216
_ pos: Int,
217217
_ count: Int?,
218218
_ match: String?) -> EventLoopFuture<(Int, T)>
219219
{
220-
var args: [RESPValueConvertible] = [key, pos]
220+
var args: [RESPValueConvertible] = [pos]
221+
222+
if let k = key {
223+
args.insert(k, at: 0)
224+
}
221225

222226
if let m = match {
223227
args.append("match")

Tests/NIORedisTests/Commands/BasicCommandsTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ final class BasicCommandsTests: XCTestCase {
189189
XCTAssertEqual(values?[1], "2")
190190
}
191191

192+
func test_scan() throws {
193+
var dataset: [String] = .init(repeating: "", count: 10)
194+
for index in 1...15 {
195+
let key = "key\(index)\(index % 2 == 0 ? "_even" : "_odd")"
196+
dataset.append(key)
197+
_ = try connection?.set(key, to: "\(index)").wait()
198+
}
199+
200+
var (cursor, keys) = try connection?.scan(count: 5).wait() ?? (0, [])
201+
XCTAssertGreaterThanOrEqual(cursor, 0)
202+
XCTAssertGreaterThanOrEqual(keys.count, 5)
203+
204+
(_, keys) = try connection?.scan(startingFrom: cursor, count: 8).wait() ?? (0, [])
205+
XCTAssertGreaterThanOrEqual(keys.count, 8)
206+
207+
(cursor, keys) = try connection?.scan(matching: "*_odd").wait() ?? (0, [])
208+
XCTAssertGreaterThanOrEqual(cursor, 0)
209+
XCTAssertGreaterThanOrEqual(keys.count, 1)
210+
XCTAssertLessThanOrEqual(keys.count, 7)
211+
212+
(cursor, keys) = try connection?.scan(matching: "*_even*").wait() ?? (0, [])
213+
XCTAssertGreaterThanOrEqual(cursor, 0)
214+
XCTAssertGreaterThanOrEqual(keys.count, 1)
215+
XCTAssertLessThanOrEqual(keys.count, 7)
216+
}
217+
192218
static var allTests = [
193219
("test_select", test_select),
194220
("test_set", test_set),
@@ -206,5 +232,6 @@ final class BasicCommandsTests: XCTestCase {
206232
("test_mget", test_mget),
207233
("test_mset", test_mset),
208234
("test_msetnx", test_msetnx),
235+
("test_scan", test_scan),
209236
]
210237
}

0 commit comments

Comments
 (0)