@@ -88,4 +88,43 @@ def test_missing_peer_columns
8888 end
8989 end
9090 end
91+
92+ # Test for repreparing statements on another host
93+ #
94+ # test_can_reprepare_statements_automatically tests that prepared statements are automatically reprepared on a host
95+ # if that host does not already have the prepared statement in its cache. It first creates a simple keyspace
96+ # and table to be used. It then prepares an insert statement on node2, by keeping node1 down. It then brings node1
97+ # back up but brings node2 down. Finally it executes the prepared statement on node1, and verifies that the query
98+ # is executed successfully using node1.
99+ #
100+ # @since 3.1.0
101+ # @jira_ticket RUBY-257
102+ # @expected_result Node1 should be able to be used to execute the prepared statement
103+ #
104+ # @test_assumptions A 2-node Cassandra cluster.
105+ # @test_category prepared_statements:preparation
106+ #
107+ def test_can_reprepare_statements_automatically
108+ cluster = Cassandra . cluster
109+ session = cluster . connect
110+
111+ session . execute ( "CREATE KEYSPACE simplex WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}" )
112+ session . execute ( "USE simplex" )
113+ session . execute ( "CREATE TABLE test (k int, v int, PRIMARY KEY (k, v))" )
114+
115+ # Prepare on node2
116+ @@ccm_cluster . stop_node ( 'node1' )
117+ insert = session . prepare ( "INSERT INTO test (k,v) VALUES (?, ?)" )
118+ assert_equal 1 , insert . execution_info . hosts . size
119+ assert_equal '127.0.0.2' , insert . execution_info . hosts . first . ip . to_s
120+ @@ccm_cluster . start_node ( 'node1' )
121+
122+ # Insert using node1
123+ @@ccm_cluster . stop_node ( 'node2' )
124+ info = session . execute ( insert , arguments : [ 0 , 0 ] ) . execution_info
125+ assert_equal 1 , info . hosts . size
126+ assert_equal '127.0.0.1' , info . hosts . first . ip . to_s
127+ ensure
128+ cluster . close
129+ end
91130end
0 commit comments