@@ -21,7 +21,15 @@ namespace Akka.Persistence.Sqlite.Tests
21
21
{
22
22
public class CustomObjectSerializerSpec : Akka . TestKit . Xunit2 . TestKit , IAsyncLifetime
23
23
{
24
- private static readonly Config Config = ConfigurationFactory . ParseString ( $@ "
24
+ private static readonly string ConnectionString ;
25
+ private static readonly Config Config ;
26
+ static CustomObjectSerializerSpec ( )
27
+ {
28
+ var filename = $ "AkkaSqlite-{ Guid . NewGuid ( ) } .db";
29
+ File . Copy ( "./data/Sqlite.CustomObject.db" , $ "{ filename } .db") ;
30
+
31
+ ConnectionString = $ "DataSource={ filename } .db";
32
+ Config = ConfigurationFactory . ParseString ( $@ "
25
33
akka.actor {{
26
34
serializers {{
27
35
mySerializer = ""{ typeof ( MySerializer ) . AssemblyQualifiedName } ""
@@ -35,18 +43,19 @@ public class CustomObjectSerializerSpec : Akka.TestKit.Xunit2.TestKit, IAsyncLif
35
43
journal {{
36
44
plugin = ""akka.persistence.journal.sqlite""
37
45
sqlite {{
38
- connection-string = ""DataSource=AkkaJournal.db ""
46
+ connection-string = ""{ ConnectionString } ""
39
47
auto-initialize = on
40
48
}}
41
49
}}
42
50
snapshot-store {{
43
51
plugin = ""akka.persistence.snapshot-store.sqlite""
44
52
sqlite {{
45
- connection-string = ""DataSource=AkkaSnapshot.db ""
53
+ connection-string = ""{ ConnectionString } ""
46
54
auto-initialize = on
47
55
}}
48
56
}}
49
57
}}" ) . WithFallback ( SqlitePersistence . DefaultConfiguration ( ) ) ;
58
+ }
50
59
51
60
public CustomObjectSerializerSpec ( ITestOutputHelper helper )
52
61
: base ( Config , nameof ( CustomObjectSerializerSpec ) , helper )
@@ -62,12 +71,13 @@ public async Task CustomSerializerTest()
62
71
var serializer = Sys . Serialization . FindSerializerForType ( typeof ( Persisted ) ) ;
63
72
serializer . Should ( ) . BeOfType < MySerializer > ( ) ;
64
73
65
- var actor = Sys . ActorOf ( Props . Create ( ( ) => new PersistedActor ( "a" ) ) ) ;
74
+ var actor = Sys . ActorOf ( Props . Create ( ( ) => new PersistedActor ( "a" , probe ) ) ) ;
75
+ probe . ExpectMsg ( "recovered" ) ;
66
76
actor . Tell ( new Persisted ( "a" ) , probe ) ;
67
77
probe . ExpectMsg ( new Persisted ( "a" ) ) ;
68
78
69
79
// Read the database directly, make sure that we're using the correct object type serializer
70
- var conn = new SqliteConnection ( "DataSource=AkkaJournal.db" ) ;
80
+ var conn = new SqliteConnection ( ConnectionString ) ;
71
81
conn . Open ( ) ;
72
82
const string sql = "SELECT ej.serializer_id FROM event_journal ej WHERE ej.persistence_id = 'a'" ;
73
83
await using var cmd = new SqliteCommand ( sql , conn ) ;
@@ -78,10 +88,19 @@ public async Task CustomSerializerTest()
78
88
record [ 0 ] . Should ( ) . Be ( 9999 ) ;
79
89
}
80
90
91
+ [ Fact ( DisplayName = "Persistence.Sql should be able to read legacy data" ) ]
92
+ public void LegacyDataTest ( )
93
+ {
94
+ var probe = CreateTestProbe ( ) ;
95
+ var actor = Sys . ActorOf ( Props . Create ( ( ) => new PersistedActor ( "old" , probe ) ) ) ;
96
+ probe . ExpectMsg ( new Persisted ( "old" ) ) ;
97
+ probe . ExpectMsg ( "recovered" ) ;
98
+ }
99
+
81
100
public Task InitializeAsync ( )
82
101
{
83
- if ( File . Exists ( "AkkaJournal .db" ) )
84
- File . Delete ( "AkkaJournal .db" ) ;
102
+ if ( File . Exists ( "AkkaSqlite .db" ) )
103
+ File . Delete ( "AkkaSqlite .db" ) ;
85
104
return Task . CompletedTask ;
86
105
}
87
106
@@ -140,9 +159,12 @@ public override object FromBinary(byte[] bytes, Type type)
140
159
141
160
internal sealed class PersistedActor : UntypedPersistentActor
142
161
{
143
- public PersistedActor ( string persistenceId )
162
+ private readonly IActorRef _probe ;
163
+
164
+ public PersistedActor ( string persistenceId , IActorRef probe )
144
165
{
145
166
PersistenceId = persistenceId ;
167
+ _probe = probe ;
146
168
}
147
169
148
170
public override string PersistenceId { get ; }
@@ -158,6 +180,15 @@ protected override void OnCommand(object message)
158
180
159
181
protected override void OnRecover ( object message )
160
182
{
183
+ switch ( message )
184
+ {
185
+ case Persisted msg :
186
+ _probe . Tell ( msg ) ;
187
+ break ;
188
+ case RecoveryCompleted _:
189
+ _probe . Tell ( "recovered" ) ;
190
+ break ;
191
+ }
161
192
}
162
193
}
163
194
}
0 commit comments