@@ -306,6 +306,134 @@ final class MemcachedIntegrationTest: XCTestCase {
306
306
}
307
307
}
308
308
309
+ func testAddValue( ) async throws {
310
+ let group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
311
+ defer {
312
+ XCTAssertNoThrow ( try ! group. syncShutdownGracefully ( ) )
313
+ }
314
+ let memcachedConnection = MemcachedConnection ( host: " memcached " , port: 11211 , eventLoopGroup: group)
315
+
316
+ try await withThrowingTaskGroup ( of: Void . self) { group in
317
+ group. addTask { try await memcachedConnection. run ( ) }
318
+
319
+ // Add a value to a key
320
+ let addValue = " foo "
321
+
322
+ // Attempt to delete the key, but ignore the error if it doesn't exist
323
+ do {
324
+ try await memcachedConnection. delete ( " adds " )
325
+ } catch {
326
+ if " \( error) " != " keyNotFound " {
327
+ throw error
328
+ }
329
+ }
330
+
331
+ // Proceed with adding the key-value pair
332
+ try await memcachedConnection. add ( " adds " , value: addValue)
333
+
334
+ // Get value for the key after add operation
335
+ let addedValue : String ? = try await memcachedConnection. get ( " adds " )
336
+ XCTAssertEqual ( addedValue, addValue, " Received value should be the same as the added value " )
337
+
338
+ group. cancelAll ( )
339
+ }
340
+ }
341
+
342
+ func testAddValueKeyExists( ) async throws {
343
+ let group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
344
+ defer {
345
+ XCTAssertNoThrow ( try ! group. syncShutdownGracefully ( ) )
346
+ }
347
+ let memcachedConnection = MemcachedConnection ( host: " memcached " , port: 11211 , eventLoopGroup: group)
348
+
349
+ try await withThrowingTaskGroup ( of: Void . self) { group in
350
+ group. addTask { try await memcachedConnection. run ( ) }
351
+
352
+ // Add a value to a key
353
+ let initialValue = " foo "
354
+ let newValue = " bar "
355
+
356
+ // Attempt to delete the key, but ignore the error if it doesn't exist
357
+ do {
358
+ try await memcachedConnection. delete ( " adds " )
359
+ } catch {
360
+ if " \( error) " != " keyNotFound " {
361
+ throw error
362
+ }
363
+ }
364
+
365
+ // Set an initial value for the key
366
+ try await memcachedConnection. add ( " adds " , value: initialValue)
367
+
368
+ do {
369
+ // Attempt to add a new value to the existing key
370
+ try await memcachedConnection. add ( " adds " , value: newValue)
371
+ XCTFail ( " Expected an error indicating the key exists, but no error was thrown. " )
372
+ } catch {
373
+ // Check if the error description or localized description matches the expected error
374
+ if " \( error) " != " keyExist " {
375
+ XCTFail ( " Unexpected error: \( error) " )
376
+ }
377
+ }
378
+
379
+ group. cancelAll ( )
380
+ }
381
+ }
382
+
383
+ func testReplaceValue( ) async throws {
384
+ let group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
385
+ defer {
386
+ XCTAssertNoThrow ( try ! group. syncShutdownGracefully ( ) )
387
+ }
388
+ let memcachedConnection = MemcachedConnection ( host: " memcached " , port: 11211 , eventLoopGroup: group)
389
+
390
+ try await withThrowingTaskGroup ( of: Void . self) { group in
391
+ group. addTask { try await memcachedConnection. run ( ) }
392
+
393
+ // Set key and initial value
394
+ let initialValue = " foo "
395
+ try await memcachedConnection. set ( " greet " , value: initialValue)
396
+
397
+ // Replace value for the key
398
+ let replaceValue = " hi "
399
+ try await memcachedConnection. replace ( " greet " , value: replaceValue)
400
+
401
+ // Get value for the key after replace operation
402
+ let replacedValue : String ? = try await memcachedConnection. get ( " greet " )
403
+ XCTAssertEqual ( replacedValue, replaceValue, " Received value should be the same as the replaceValue " )
404
+
405
+ group. cancelAll ( )
406
+ }
407
+ }
408
+
409
+ func testReplaceNonExistentKey( ) async throws {
410
+ let group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
411
+ defer {
412
+ XCTAssertNoThrow ( try ! group. syncShutdownGracefully ( ) )
413
+ }
414
+ let memcachedConnection = MemcachedConnection ( host: " memcached " , port: 11211 , eventLoopGroup: group)
415
+
416
+ await withThrowingTaskGroup ( of: Void . self) { group in
417
+ group. addTask { try await memcachedConnection. run ( ) }
418
+
419
+ do {
420
+ // Ensure the key is clean
421
+ try await memcachedConnection. delete ( " nonExistentKey " )
422
+ // Attempt to replace value for a non-existent key
423
+ let replaceValue = " testValue "
424
+ try await memcachedConnection. replace ( " nonExistentKey " , value: replaceValue)
425
+ XCTFail ( " Expected an error indicating the key was not found, but no error was thrown. " )
426
+ } catch {
427
+ // Check if the error description or localized description matches the expected error
428
+ if " \( error) " != " keyNotFound " {
429
+ XCTFail ( " Unexpected error: \( error) " )
430
+ }
431
+ }
432
+
433
+ group. cancelAll ( )
434
+ }
435
+ }
436
+
309
437
func testMemcachedConnectionWithUInt( ) async throws {
310
438
let group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
311
439
defer {
0 commit comments