@@ -761,26 +761,34 @@ public void testType() {
761
761
assertEquals (DataType .STRING , redisTemplate .type (key1 ));
762
762
}
763
763
764
+ /**
765
+ * @see DATAREDIS-506
766
+ */
764
767
@ Test
765
768
public void testWatch () {
766
769
final K key1 = keyFactory .instance ();
767
770
V value1 = valueFactory .instance ();
768
771
final V value2 = valueFactory .instance ();
769
772
final V value3 = valueFactory .instance ();
770
773
redisTemplate .opsForValue ().set (key1 , value1 );
774
+
771
775
final Thread th = new Thread (new Runnable () {
772
776
public void run () {
773
777
redisTemplate .opsForValue ().set (key1 , value2 );
774
778
}
775
779
});
780
+
776
781
List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
777
782
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
778
783
public List <Object > execute (RedisOperations operations ) throws DataAccessException {
784
+
779
785
operations .watch (key1 );
786
+
780
787
th .start ();
781
788
try {
782
789
th .join ();
783
790
} catch (InterruptedException e ) {}
791
+
784
792
operations .multi ();
785
793
operations .opsForValue ().set (key1 , value3 );
786
794
return operations .exec ();
@@ -792,11 +800,13 @@ public List<Object> execute(RedisOperations operations) throws DataAccessExcepti
792
800
} else {
793
801
assertNull (results );
794
802
}
803
+
795
804
assertThat (redisTemplate .opsForValue ().get (key1 ), isEqual (value2 ));
796
805
}
797
806
798
807
@ Test
799
808
public void testUnwatch () {
809
+
800
810
final K key1 = keyFactory .instance ();
801
811
V value1 = valueFactory .instance ();
802
812
final V value2 = valueFactory .instance ();
@@ -807,48 +817,62 @@ public void run() {
807
817
redisTemplate .opsForValue ().set (key1 , value2 );
808
818
}
809
819
});
820
+
810
821
List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
811
822
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
812
823
public List <Object > execute (RedisOperations operations ) throws DataAccessException {
824
+
813
825
operations .watch (key1 );
826
+
814
827
th .start ();
815
828
try {
816
829
th .join ();
817
830
} catch (InterruptedException e ) {}
831
+
818
832
operations .unwatch ();
819
833
operations .multi ();
820
834
operations .opsForValue ().set (key1 , value3 );
821
835
return operations .exec ();
822
836
}
823
837
});
838
+
824
839
assertTrue (results .isEmpty ());
825
840
assertThat (redisTemplate .opsForValue ().get (key1 ), isEqual (value3 ));
826
841
}
827
842
843
+ /**
844
+ * @see DATAREDIS-506
845
+ */
828
846
@ Test
829
847
public void testWatchMultipleKeys () {
848
+
830
849
final K key1 = keyFactory .instance ();
831
850
final K key2 = keyFactory .instance ();
832
851
V value1 = valueFactory .instance ();
833
852
final V value2 = valueFactory .instance ();
834
853
final V value3 = valueFactory .instance ();
835
854
redisTemplate .opsForValue ().set (key1 , value1 );
855
+
836
856
final Thread th = new Thread (new Runnable () {
837
857
public void run () {
838
858
redisTemplate .opsForValue ().set (key1 , value2 );
839
859
}
840
860
});
861
+
841
862
List <Object > results = redisTemplate .execute (new SessionCallback <List <Object >>() {
842
863
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
843
864
public List <Object > execute (RedisOperations operations ) throws DataAccessException {
865
+
844
866
List <K > keys = new ArrayList <K >();
845
867
keys .add (key1 );
846
868
keys .add (key2 );
847
869
operations .watch (keys );
870
+
848
871
th .start ();
849
872
try {
850
873
th .join ();
851
874
} catch (InterruptedException e ) {}
875
+
852
876
operations .multi ();
853
877
operations .opsForValue ().set (key1 , value3 );
854
878
return operations .exec ();
0 commit comments