@@ -761,26 +761,34 @@ public void testType() {
761761assertEquals (DataType .STRING , redisTemplate .type (key1 ));
762762}
763763
764+ /**
765+ * @see DATAREDIS-506
766+ */
764767@ Test
765768public void testWatch () {
766769final K key1 = keyFactory .instance ();
767770V value1 = valueFactory .instance ();
768771final V value2 = valueFactory .instance ();
769772final V value3 = valueFactory .instance ();
770773redisTemplate .opsForValue ().set (key1 , value1 );
774+
771775final Thread th = new Thread (new Runnable () {
772776public void run () {
773777redisTemplate .opsForValue ().set (key1 , value2 );
774778}
775779});
780+
776781List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
777782@ SuppressWarnings ({ "unchecked" , "rawtypes" })
778783public List <Object > execute (RedisOperations operations ) throws DataAccessException {
784+
779785operations .watch (key1 );
786+
780787th .start ();
781788try {
782789th .join ();
783790} catch (InterruptedException e ) {}
791+
784792operations .multi ();
785793operations .opsForValue ().set (key1 , value3 );
786794return operations .exec ();
@@ -792,11 +800,13 @@ public List<Object> execute(RedisOperations operations) throws DataAccessExcepti
792800} else {
793801assertNull (results );
794802}
803+
795804assertThat (redisTemplate .opsForValue ().get (key1 ), isEqual (value2 ));
796805}
797806
798807@ Test
799808public void testUnwatch () {
809+
800810final K key1 = keyFactory .instance ();
801811V value1 = valueFactory .instance ();
802812final V value2 = valueFactory .instance ();
@@ -807,48 +817,62 @@ public void run() {
807817redisTemplate .opsForValue ().set (key1 , value2 );
808818}
809819});
820+
810821List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
811822@ SuppressWarnings ({ "unchecked" , "rawtypes" })
812823public List <Object > execute (RedisOperations operations ) throws DataAccessException {
824+
813825operations .watch (key1 );
826+
814827th .start ();
815828try {
816829th .join ();
817830} catch (InterruptedException e ) {}
831+
818832operations .unwatch ();
819833operations .multi ();
820834operations .opsForValue ().set (key1 , value3 );
821835return operations .exec ();
822836}
823837});
838+
824839assertTrue (results .isEmpty ());
825840assertThat (redisTemplate .opsForValue ().get (key1 ), isEqual (value3 ));
826841}
827842
843+ /**
844+ * @see DATAREDIS-506
845+ */
828846@ Test
829847public void testWatchMultipleKeys () {
848+
830849final K key1 = keyFactory .instance ();
831850final K key2 = keyFactory .instance ();
832851V value1 = valueFactory .instance ();
833852final V value2 = valueFactory .instance ();
834853final V value3 = valueFactory .instance ();
835854redisTemplate .opsForValue ().set (key1 , value1 );
855+
836856final Thread th = new Thread (new Runnable () {
837857public void run () {
838858redisTemplate .opsForValue ().set (key1 , value2 );
839859}
840860});
861+
841862List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
842863@ SuppressWarnings ({ "unchecked" , "rawtypes" })
843864public List <Object > execute (RedisOperations operations ) throws DataAccessException {
865+
844866List <K > keys = new ArrayList <K >();
845867keys .add (key1 );
846868keys .add (key2 );
847869operations .watch (keys );
870+
848871th .start ();
849872try {
850873th .join ();
851874} catch (InterruptedException e ) {}
875+
852876operations .multi ();
853877operations .opsForValue ().set (key1 , value3 );
854878return operations .exec ();
0 commit comments