@@ -54,6 +54,24 @@ struct ClusterIntegrationTests {
5454 }
5555 }
5656
57+ @Test
58+ @available ( valkeySwift 1 . 0 , * )
59+ func testWithReadOnlyConnection( ) async throws {
60+ var logger = Logger ( label: " ValkeyCluster " )
61+ logger. logLevel = . trace
62+ let firstNodeHostname = clusterFirstNodeHostname!
63+ let firstNodePort = clusterFirstNodePort ?? 6379
64+ try await Self . withValkeyCluster ( [ ( host: firstNodeHostname, port: firstNodePort) ] , logger: logger) { client in
65+ try await Self . withKey ( connection: client) { key in
66+ try await client. set ( key, value: " Hello " )
67+ try await client. withConnection ( forKeys: [ key] , readOnly: true ) { connection in
68+ let response = try await connection. get ( key)
69+ #expect( response. map { String ( buffer: $0) } == " Hello " )
70+ }
71+ }
72+ }
73+ }
74+
5775 @Test
5876 @available ( valkeySwift 1 . 0 , * )
5977 func testFailover( ) async throws {
@@ -270,11 +288,11 @@ struct ClusterIntegrationTests {
270288 afterMigrate: ( ) async throws -> Void = { } ,
271289 finished: ( ) async throws -> Void = { }
272290 ) async throws {
273- let nodeAClient = try await client. nodeClient ( for: [ hashSlot] )
291+ let nodeAClient = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
274292 // find another shard
275293 var nodeBClient : ValkeyNodeClient
276294 repeat {
277- nodeBClient = try await client. nodeClient ( for: [ HashSlot ( rawValue: Int . random ( in: 0 ..< 16384 ) ) !] )
295+ nodeBClient = try await client. nodeClient ( for: [ HashSlot ( rawValue: Int . random ( in: 0 ..< 16384 ) ) !] , readOnly : false )
278296 } while nodeAClient === nodeBClient
279297
280298 guard let ( hostnameA, portA) = nodeAClient. serverAddress. getHostnameAndPort ( ) else { return }
@@ -341,7 +359,7 @@ struct ClusterIntegrationTests {
341359 try await ClusterIntegrationTests . withValkeyCluster ( [ ( host: firstNodeHostname, port: firstNodePort) ] , logger: logger) {
342360 client in
343361 try await ClusterIntegrationTests . withKey ( connection: client, suffix: " {foo} " ) { key in
344- let node = try await client. nodeClient ( for: [ HashSlot ( key: key) ] )
362+ let node = try await client. nodeClient ( for: [ HashSlot ( key: key) ] , readOnly : false )
345363 var commands : [ any ValkeyCommand ] = . init( )
346364 commands. append ( SET ( key, value: " cluster pipeline test " ) )
347365 commands. append ( GET ( key) )
@@ -363,13 +381,13 @@ struct ClusterIntegrationTests {
363381 client in
364382 try await ClusterIntegrationTests . withKey ( connection: client, suffix: " {foo} " ) { key in
365383 let hashSlot = HashSlot ( key: key)
366- let node = try await client. nodeClient ( for: [ hashSlot] )
384+ let node = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
367385 // get a key from same node
368386 let key2 = try await {
369387 while true {
370388 let key2 = ValkeyKey ( UUID ( ) . uuidString)
371389 let hashSlot2 = HashSlot ( key: key2)
372- let node2 = try await client. nodeClient ( for: [ hashSlot2] )
390+ let node2 = try await client. nodeClient ( for: [ hashSlot2] , readOnly : false )
373391 if node2. serverAddress == node. serverAddress {
374392 return key2
375393 }
@@ -400,12 +418,12 @@ struct ClusterIntegrationTests {
400418 client in
401419 try await ClusterIntegrationTests . withKey ( connection: client, suffix: " {foo} " ) { key in
402420 let hashSlot = HashSlot ( key: key)
403- let node = try await client. nodeClient ( for: [ hashSlot] )
421+ let node = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
404422 let key2 = try await {
405423 while true {
406424 let key2 = ValkeyKey ( UUID ( ) . uuidString)
407425 let hashSlot2 = HashSlot ( key: key2)
408- let node2 = try await client. nodeClient ( for: [ hashSlot2] )
426+ let node2 = try await client. nodeClient ( for: [ hashSlot2] , readOnly : false )
409427 if node2. serverAddress != node. serverAddress {
410428 return key2
411429 }
@@ -435,12 +453,12 @@ struct ClusterIntegrationTests {
435453 client in
436454 try await ClusterIntegrationTests . withKey ( connection: client, suffix: " {foo} " ) { key in
437455 let hashSlot = HashSlot ( key: key)
438- let node = try await client. nodeClient ( for: [ hashSlot] )
456+ let node = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
439457 let key2 = try await {
440458 while true {
441459 let key2 = ValkeyKey ( UUID ( ) . uuidString)
442460 let hashSlot2 = HashSlot ( key: key2)
443- let node2 = try await client. nodeClient ( for: [ hashSlot2] )
461+ let node2 = try await client. nodeClient ( for: [ hashSlot2] , readOnly : false )
444462 if node2. serverAddress != node. serverAddress {
445463 return key2
446464 }
@@ -470,7 +488,7 @@ struct ClusterIntegrationTests {
470488 try await ClusterIntegrationTests . withValkeyCluster ( [ ( host: firstNodeHostname, port: firstNodePort) ] , logger: logger) {
471489 clusterClient in
472490 try await ClusterIntegrationTests . withKey ( connection: clusterClient) { key in
473- let node = try await clusterClient. nodeClient ( for: [ HashSlot ( key: key) ] )
491+ let node = try await clusterClient. nodeClient ( for: [ HashSlot ( key: key) ] , readOnly : false )
474492 try await clusterClient. set ( key, value: " bar " )
475493 let cluster = try await clusterClient. clusterShards ( )
476494 let shard = try #require(
@@ -512,7 +530,7 @@ struct ClusterIntegrationTests {
512530 let keySuffix = " { \( UUID ( ) . uuidString) } "
513531 try await ClusterIntegrationTests . withKey ( connection: client, suffix: keySuffix) { key in
514532 let hashSlot = HashSlot ( key: key)
515- let node = try await client. nodeClient ( for: [ hashSlot] )
533+ let node = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
516534 try await client. set ( key, value: " Testing before import " )
517535
518536 try await ClusterIntegrationTests . testMigratingHashSlot ( hashSlot, client: client) {
@@ -548,7 +566,7 @@ struct ClusterIntegrationTests {
548566 try await ClusterIntegrationTests . withKey ( connection: client, suffix: keySuffix) { key in
549567 try await ClusterIntegrationTests . withKey ( connection: client, suffix: keySuffix) { key2 in
550568 let hashSlot = HashSlot ( key: key)
551- let node = try await client. nodeClient ( for: [ hashSlot] )
569+ let node = try await client. nodeClient ( for: [ hashSlot] , readOnly : false )
552570 try await client. lpush ( key, elements: [ " testing1 " ] )
553571
554572 try await ClusterIntegrationTests . testMigratingHashSlot ( hashSlot, client: client) {
@@ -820,7 +838,7 @@ struct ClusterIntegrationTests {
820838 @available ( valkeySwift 1 . 0 , * )
821839 static func withValkeyCluster< T> (
822840 _ nodeAddresses: [ ( host: String , port: Int ) ] ,
823- nodeClientConfiguration: ValkeyClientConfiguration = . init( ) ,
841+ nodeClientConfiguration: ValkeyClientConfiguration = . init( useReadOnlyReplicas : true ) ,
824842 logger: Logger ,
825843 _ body: ( ValkeyClusterClient ) async throws -> sending T
826844 ) async throws -> T {
0 commit comments