@@ -50,15 +50,6 @@ impl SharedMemorySnapshot {
5050 } )
5151 }
5252
53- /// Take another snapshot of the internally-stored `SharedMemory`,
54- /// then store it internally.
55- #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level= "Trace" ) ]
56- #[ allow( dead_code) ]
57- pub ( super ) fn replace_snapshot < S : SharedMemory > ( & mut self , shared_mem : & mut S ) -> Result < ( ) > {
58- self . snapshot = shared_mem. with_exclusivity ( |e| e. copy_all_to_vec ( ) ) ??;
59- Ok ( ( ) )
60- }
61-
6253 /// Copy the memory from the internally-stored memory snapshot
6354 /// into the internally-stored `SharedMemory`.
6455 #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level= "Trace" ) ]
@@ -91,36 +82,58 @@ mod tests {
9182 use crate :: mem:: shared_mem:: ExclusiveSharedMemory ;
9283
9384 #[ test]
94- fn restore_replace ( ) {
95- let mut data1 = vec ! [ b'a' , b'b' , b'c' ] ;
96- data1. resize_with ( PAGE_SIZE_USIZE , || 0 ) ;
97- let data2 = data1. iter ( ) . map ( |b| b + 1 ) . collect :: < Vec < u8 > > ( ) ;
85+ fn restore ( ) {
86+ // Simplified version of the original test
87+ let data1 = vec ! [ b'a' ; PAGE_SIZE_USIZE ] ;
88+ let data2 = vec ! [ b'b' ; PAGE_SIZE_USIZE ] ;
89+
9890 let mut gm = ExclusiveSharedMemory :: new ( PAGE_SIZE_USIZE ) . unwrap ( ) ;
99- gm. copy_from_slice ( data1. as_slice ( ) , 0 ) . unwrap ( ) ;
100- let mut snap = super :: SharedMemorySnapshot :: new ( & mut gm, 0 , Vec :: new ( ) ) . unwrap ( ) ;
101- {
102- // after the first snapshot is taken, make sure gm has the equivalent
103- // of data1
104- assert_eq ! ( data1, gm. copy_all_to_vec( ) . unwrap( ) ) ;
105- }
106-
107- {
108- // modify gm with data2 rather than data1 and restore from
109- // snapshot. we should have the equivalent of data1 again
110- gm. copy_from_slice ( data2. as_slice ( ) , 0 ) . unwrap ( ) ;
111- assert_eq ! ( data2, gm. copy_all_to_vec( ) . unwrap( ) ) ;
112- snap. restore_from_snapshot ( & mut gm) . unwrap ( ) ;
113- assert_eq ! ( data1, gm. copy_all_to_vec( ) . unwrap( ) ) ;
114- }
115- {
116- // modify gm with data2, then retake the snapshot and restore
117- // from the new snapshot. we should have the equivalent of data2
118- gm. copy_from_slice ( data2. as_slice ( ) , 0 ) . unwrap ( ) ;
119- assert_eq ! ( data2, gm. copy_all_to_vec( ) . unwrap( ) ) ;
120- snap. replace_snapshot ( & mut gm) . unwrap ( ) ;
121- assert_eq ! ( data2, gm. copy_all_to_vec( ) . unwrap( ) ) ;
122- snap. restore_from_snapshot ( & mut gm) . unwrap ( ) ;
123- assert_eq ! ( data2, gm. copy_all_to_vec( ) . unwrap( ) ) ;
124- }
91+ gm. copy_from_slice ( & data1, 0 ) . unwrap ( ) ;
92+
93+ // Take snapshot of data1
94+ let snapshot = super :: SharedMemorySnapshot :: new ( & mut gm, 0 , Vec :: new ( ) ) . unwrap ( ) ;
95+
96+ // Modify memory to data2
97+ gm. copy_from_slice ( & data2, 0 ) . unwrap ( ) ;
98+ assert_eq ! ( gm. as_slice( ) , & data2[ ..] ) ;
99+
100+ // Restore should bring back data1
101+ snapshot. restore_from_snapshot ( & mut gm) . unwrap ( ) ;
102+ assert_eq ! ( gm. as_slice( ) , & data1[ ..] ) ;
103+ }
104+
105+ #[ test]
106+ fn snapshot_mem_size ( ) {
107+ let size = PAGE_SIZE_USIZE * 2 ;
108+ let mut gm = ExclusiveSharedMemory :: new ( size) . unwrap ( ) ;
109+
110+ let snapshot = super :: SharedMemorySnapshot :: new ( & mut gm, 0 , Vec :: new ( ) ) . unwrap ( ) ;
111+ assert_eq ! ( snapshot. mem_size( ) , size) ;
112+ }
113+
114+ #[ test]
115+ fn multiple_snapshots_independent ( ) {
116+ let mut gm = ExclusiveSharedMemory :: new ( PAGE_SIZE_USIZE ) . unwrap ( ) ;
117+
118+ // Create first snapshot with pattern A
119+ let pattern_a = vec ! [ 0xAA ; PAGE_SIZE_USIZE ] ;
120+ gm. copy_from_slice ( & pattern_a, 0 ) . unwrap ( ) ;
121+ let snapshot_a = super :: SharedMemorySnapshot :: new ( & mut gm, 1 , Vec :: new ( ) ) . unwrap ( ) ;
122+
123+ // Create second snapshot with pattern B
124+ let pattern_b = vec ! [ 0xBB ; PAGE_SIZE_USIZE ] ;
125+ gm. copy_from_slice ( & pattern_b, 0 ) . unwrap ( ) ;
126+ let snapshot_b = super :: SharedMemorySnapshot :: new ( & mut gm, 2 , Vec :: new ( ) ) . unwrap ( ) ;
127+
128+ // Clear memory
129+ gm. copy_from_slice ( & [ 0 ; PAGE_SIZE_USIZE ] , 0 ) . unwrap ( ) ;
130+
131+ // Restore snapshot A
132+ snapshot_a. restore_from_snapshot ( & mut gm) . unwrap ( ) ;
133+ assert_eq ! ( gm. as_slice( ) , & pattern_a[ ..] ) ;
134+
135+ // Restore snapshot B
136+ snapshot_b. restore_from_snapshot ( & mut gm) . unwrap ( ) ;
137+ assert_eq ! ( gm. as_slice( ) , & pattern_b[ ..] ) ;
125138 }
126139}
0 commit comments