@@ -17,25 +17,19 @@ struct CleanStrategyTests {
1717 let container = TestModelBuilder . createContainer (
1818 author: " App1 " ,
1919 testName: " noneStrategyDisablesAutomaticCleanup " )
20- let context1 = container. viewContext
21- context1. transactionAuthor = " App1 "
20+ let writer = TestAppDataHandler ( container: container, viewName: " Writer " )
2221
23- TestModelBuilder . createPerson ( name: " Alice " , age: 30 , in: context1)
24- try context1. save ( )
22+ try await writer. createPerson ( name: " Alice " , age: 30 , author: " App1 " )
2523
2624 try await Task . sleep ( nanoseconds: 100_000_000 )
2725
28- TestModelBuilder . createPerson ( name: " Bob " , age: 31 , in: context1)
29- try context1. save ( )
26+ try await writer. createPerson ( name: " Bob " , age: 31 , author: " App1 " )
3027
3128 let processor = makeProcessor ( container: container, cleanStrategy: . none)
32- let mergeContext = container. newBackgroundContext ( )
33- mergeContext. transactionAuthor = " App2 "
3429
3530 _ = try await processor. processNewTransactionsWithTimestampManagement (
3631 from: [ " App1 " , " App2 " ] ,
3732 after: nil ,
38- mergeInto: [ mergeContext] ,
3933 currentAuthor: " App2 " )
4034
4135 let remainingCount = try await processor. testTransactionCount (
@@ -51,30 +45,24 @@ struct CleanStrategyTests {
5145 author: " App1 " ,
5246 testName: " durationStrategyThrottlesCleanup " )
5347 let userDefaults = TestModelBuilder . createTestUserDefaults ( )
54- let context1 = container. viewContext
55- context1. transactionAuthor = " App1 "
48+ let writer = TestAppDataHandler ( container: container, viewName: " Writer " )
5649
57- TestModelBuilder . createPerson ( name: " Alice " , age: 30 , in: context1)
58- try context1. save ( )
50+ try await writer. createPerson ( name: " Alice " , age: 30 , author: " App1 " )
5951
6052 try await Task . sleep ( nanoseconds: 100_000_000 )
6153
62- TestModelBuilder . createPerson ( name: " Bob " , age: 31 , in: context1)
63- try context1. save ( )
54+ try await writer. createPerson ( name: " Bob " , age: 31 , author: " App1 " )
6455
6556 let processor = makeProcessor (
6657 container: container,
6758 userDefaults: userDefaults,
6859 cleanStrategy: . byDuration( seconds: 60 * 60 ) )
69- let mergeContext = container. newBackgroundContext ( )
70- mergeContext. transactionAuthor = " App2 "
7160
7261 userDefaults. set ( Date ( ) , forKey: " PersistentHistoryTrackingKit.lastToken.App1 " )
7362
7463 _ = try await processor. processNewTransactionsWithTimestampManagement (
7564 from: [ " App1 " , " App2 " ] ,
7665 after: nil ,
77- mergeInto: [ mergeContext] ,
7866 currentAuthor: " App2 " )
7967
8068 let countAfterFirstProcess = try await processor. testTransactionCount (
@@ -87,13 +75,11 @@ struct CleanStrategyTests {
8775
8876 try await Task . sleep ( nanoseconds: 100_000_000 )
8977
90- TestModelBuilder . createPerson ( name: " Charlie " , age: 32 , in: context1)
91- try context1. save ( )
78+ try await writer. createPerson ( name: " Charlie " , age: 32 , author: " App1 " )
9279
9380 _ = try await processor. processNewTransactionsWithTimestampManagement (
9481 from: [ " App1 " , " App2 " ] ,
9582 after: lastTimestamp,
96- mergeInto: [ mergeContext] ,
9783 currentAuthor: " App2 " )
9884
9985 let countAfterSecondProcess = try await processor. testTransactionCount (
@@ -108,30 +94,24 @@ struct CleanStrategyTests {
10894 author: " App1 " ,
10995 testName: " notificationStrategyCleansOnConfiguredCount " )
11096 let userDefaults = TestModelBuilder . createTestUserDefaults ( )
111- let context1 = container. viewContext
112- context1. transactionAuthor = " App1 "
97+ let writer = TestAppDataHandler ( container: container, viewName: " Writer " )
11398
114- TestModelBuilder . createPerson ( name: " Alice " , age: 30 , in: context1)
115- try context1. save ( )
99+ try await writer. createPerson ( name: " Alice " , age: 30 , author: " App1 " )
116100
117101 try await Task . sleep ( nanoseconds: 100_000_000 )
118102
119- TestModelBuilder . createPerson ( name: " Bob " , age: 31 , in: context1)
120- try context1. save ( )
103+ try await writer. createPerson ( name: " Bob " , age: 31 , author: " App1 " )
121104
122105 let processor = makeProcessor (
123106 container: container,
124107 userDefaults: userDefaults,
125108 cleanStrategy: . byNotification( times: 2 ) )
126- let mergeContext = container. newBackgroundContext ( )
127- mergeContext. transactionAuthor = " App2 "
128109
129110 userDefaults. set ( Date ( ) , forKey: " PersistentHistoryTrackingKit.lastToken.App1 " )
130111
131112 _ = try await processor. processNewTransactionsWithTimestampManagement (
132113 from: [ " App1 " , " App2 " ] ,
133114 after: nil ,
134- mergeInto: [ mergeContext] ,
135115 currentAuthor: " App2 " )
136116
137117 let countAfterFirstProcess = try await processor. testTransactionCount (
@@ -144,13 +124,11 @@ struct CleanStrategyTests {
144124
145125 try await Task . sleep ( nanoseconds: 100_000_000 )
146126
147- TestModelBuilder . createPerson ( name: " Charlie " , age: 32 , in: context1)
148- try context1. save ( )
127+ try await writer. createPerson ( name: " Charlie " , age: 32 , author: " App1 " )
149128
150129 _ = try await processor. processNewTransactionsWithTimestampManagement (
151130 from: [ " App1 " , " App2 " ] ,
152131 after: lastTimestamp,
153- mergeInto: [ mergeContext] ,
154132 currentAuthor: " App2 " )
155133
156134 let countAfterSecondProcess = try await processor. testTransactionCount (
@@ -164,27 +142,21 @@ struct CleanStrategyTests {
164142 let container = TestModelBuilder . createContainer (
165143 author: " App1 " ,
166144 testName: " automaticCleanupWaitsForMissingAuthorTimestamps " )
167- let context1 = container. viewContext
168- context1. transactionAuthor = " App1 "
145+ let writer = TestAppDataHandler ( container: container, viewName: " Writer " )
169146
170- TestModelBuilder . createPerson ( name: " Alice " , age: 30 , in: context1)
171- try context1. save ( )
147+ try await writer. createPerson ( name: " Alice " , age: 30 , author: " App1 " )
172148
173149 try await Task . sleep ( nanoseconds: 100_000_000 )
174150
175- TestModelBuilder . createPerson ( name: " Bob " , age: 31 , in: context1)
176- try context1. save ( )
151+ try await writer. createPerson ( name: " Bob " , age: 31 , author: " App1 " )
177152
178153 let processor = makeProcessor (
179154 container: container,
180155 cleanStrategy: . byDuration( seconds: 60 * 60 ) )
181- let mergeContext = container. newBackgroundContext ( )
182- mergeContext. transactionAuthor = " App2 "
183156
184157 _ = try await processor. processNewTransactionsWithTimestampManagement (
185158 from: [ " App1 " , " App2 " , " App3 " ] ,
186159 after: nil ,
187- mergeInto: [ mergeContext] ,
188160 currentAuthor: " App2 " )
189161
190162 let remainingCount = try await processor. testTransactionCount (
@@ -200,30 +172,24 @@ struct CleanStrategyTests {
200172 author: " App1 " ,
201173 testName: " batchAuthorsDoNotBlockCleanupReadiness " )
202174 let userDefaults = TestModelBuilder . createTestUserDefaults ( )
203- let context1 = container. viewContext
204- context1. transactionAuthor = " App1 "
175+ let writer = TestAppDataHandler ( container: container, viewName: " Writer " )
205176
206- TestModelBuilder . createPerson ( name: " Alice " , age: 30 , in: context1)
207- try context1. save ( )
177+ try await writer. createPerson ( name: " Alice " , age: 30 , author: " App1 " )
208178
209179 try await Task . sleep ( nanoseconds: 100_000_000 )
210180
211- TestModelBuilder . createPerson ( name: " Bob " , age: 31 , in: context1)
212- try context1. save ( )
181+ try await writer. createPerson ( name: " Bob " , age: 31 , author: " App1 " )
213182
214183 let processor = makeProcessor (
215184 container: container,
216185 userDefaults: userDefaults,
217186 cleanStrategy: . byDuration( seconds: 60 * 60 ) )
218- let mergeContext = container. newBackgroundContext ( )
219- mergeContext. transactionAuthor = " App2 "
220187
221188 userDefaults. set ( Date ( ) , forKey: " PersistentHistoryTrackingKit.lastToken.App1 " )
222189
223190 _ = try await processor. processNewTransactionsWithTimestampManagement (
224191 from: [ " App1 " , " App2 " , " BatchProcessor " ] ,
225192 after: nil ,
226- mergeInto: [ mergeContext] ,
227193 currentAuthor: " App2 " ,
228194 batchAuthors: [ " BatchProcessor " ] )
229195
@@ -245,6 +211,7 @@ struct CleanStrategyTests {
245211 maximumDuration: 604_800 )
246212 return TransactionProcessorActor (
247213 container: container,
214+ contexts: [ container. newBackgroundContext ( ) ] ,
248215 hookRegistry: hookRegistry,
249216 cleanStrategy: cleanStrategy,
250217 timestampManager: timestampManager)
0 commit comments