Skip to content
Prev Previous commit
Next Next commit
more test for add + replace
  • Loading branch information
dkz2 committed Aug 6, 2023
commit f504be3a90736249c8cde0763f0a52d910165073
13 changes: 11 additions & 2 deletions Sources/SwiftMemcache/MemcachedConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public actor MemcachedConnection {

// MARK: - Adding a Value

/// Adds a value to a new key in the Memcached server.
/// Adds a new key-value pair in the Memcached server.
/// The operation will fail if the key already exists.
///
/// - Parameters:
Expand Down Expand Up @@ -409,7 +409,16 @@ public actor MemcachedConnection {
let command = MemcachedRequest.SetCommand(key: key, value: buffer, flags: flags)
let request = MemcachedRequest.set(command)

_ = try await self.sendRequest(request)
let response = try await sendRequest(request)

switch response.returnCode {
case .HD:
return
case .NS:
throw MemcachedConnectionError.keyNotFound
default:
throw MemcachedConnectionError.unexpectedNilResponse
}

case .finished:
throw MemcachedConnectionError.connectionShutdown
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftMemcache/MemcachedFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum TimeToLive: Equatable, Hashable {
}

/// Enum representing the Memcached 'ms' (meta set) command modes (corresponding to the 'M' flag).
public enum StorageMode: Equatable, Hashable {
enum StorageMode: Equatable, Hashable {
/// The "add" command. If the item exists, LRU is bumped and NS is returned.
case add
/// The 'append' command. If the item exists, append the new value to its data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,39 @@ final class MemcachedIntegrationTest: XCTestCase {
}
}

func testAddValueKeyExists() async throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try! group.syncShutdownGracefully())
}
let memcachedConnection = MemcachedConnection(host: "memcached", port: 11211, eventLoopGroup: group)

try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask { try await memcachedConnection.run() }

// Add a value to a key
let initialValue = "foo"
let newValue = "bar"

// Ensure the key is clean and add an initial value
try await memcachedConnection.delete("adds")
try await memcachedConnection.add("adds", value: initialValue)

do {
// Attempt to add a new value to the existing key
try await memcachedConnection.add("adds", value: newValue)
XCTFail("Expected an error indicating the key exists, but no error was thrown.")
} catch {
// Check if the error description or localized description matches the expected error
if "\(error)" != "keyExist" {
XCTFail("Unexpected error: \(error)")
}
}

group.cancelAll()
}
}

func testReplaceValue() async throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
Expand All @@ -356,6 +389,34 @@ final class MemcachedIntegrationTest: XCTestCase {
}
}

func testReplaceNonExistentKey() async throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try! group.syncShutdownGracefully())
}
let memcachedConnection = MemcachedConnection(host: "memcached", port: 11211, eventLoopGroup: group)

await withThrowingTaskGroup(of: Void.self) { group in
group.addTask { try await memcachedConnection.run() }

do {
// Ensure the key is clean
try await memcachedConnection.delete("nonExistentKey")
// Attempt to replace value for a non-existent key
let replaceValue = "testValue"
try await memcachedConnection.replace("nonExistentKey", value: replaceValue)
XCTFail("Expected an error indicating the key was not found, but no error was thrown.")
} catch {
// Check if the error description or localized description matches the expected error
if "\(error)" != "keyNotFound" {
XCTFail("Unexpected error: \(error)")
}
}

group.cancelAll()
}
}

func testMemcachedConnectionWithUInt() async throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
Expand Down