@@ -1140,4 +1140,79 @@ final class SyncChangeStreamTests: MongoSwiftTestCase {
11401140 expect ( stream. isAlive ( ) ) . to ( beFalse ( ) )
11411141 }
11421142 }
1143+
1144+ /// Test that we properly manually populate the `ns` field of `ChangeStreamEvent`s for invalidate events on
1145+ /// collections.
1146+ func testDecodingInvalidateEventsOnCollection( ) throws {
1147+ // invalidated change stream on a collection
1148+ try self . withTestNamespace { client, _, collection in
1149+ let unmetRequirement = try MongoClient . makeTestClient ( ) . getUnmetRequirement (
1150+ TestRequirement ( acceptableTopologies: [ . replicaSet, . sharded] )
1151+ )
1152+ guard unmetRequirement == nil else {
1153+ printSkipMessage ( testName: self . name, unmetRequirement: unmetRequirement!)
1154+ return
1155+ }
1156+
1157+ let stream = try collection. watch ( )
1158+
1159+ // insert to create the collection
1160+ try collection. insertOne ( [ " x " : 1 ] )
1161+ let insertEvent = try stream. next ( ) ? . get ( )
1162+ expect ( insertEvent? . operationType) . to ( equal ( . insert) )
1163+
1164+ // drop the collection to generate an invalidate event
1165+ try collection. drop ( )
1166+
1167+ // as of 4.0.1, the server first emits a drop event and then an invalidate event.
1168+ // on 3.6, there is only an invalidate event.
1169+ if try client. serverVersion ( ) >= ServerVersion ( major: 4 , minor: 0 , patch: 1 ) {
1170+ let drop = try stream. next ( ) ? . get ( )
1171+ expect ( drop? . operationType) . to ( equal ( . drop) )
1172+ }
1173+
1174+ let invalidate = try stream. next ( ) ? . get ( )
1175+ expect ( invalidate? . operationType) . to ( equal ( . invalidate) )
1176+ expect ( invalidate? . ns) . to ( equal ( collection. namespace) )
1177+ }
1178+ }
1179+
1180+ /// Test that we properly manually populate the `ns` field of `ChangeStreamEvent`s for invalidate events on
1181+ /// databases.
1182+ func testDecodingInvalidateEventsOnDatabase( ) throws {
1183+ // invalidated change stream on a DB
1184+ try self . withTestNamespace { client, db, collection in
1185+ // DB change streams are supported as of 4.0
1186+ let unmetRequirement = try client. getUnmetRequirement (
1187+ TestRequirement (
1188+ minServerVersion: ServerVersion ( major: 4 , minor: 0 ) ,
1189+ acceptableTopologies: [ . replicaSet, . sharded]
1190+ )
1191+ )
1192+ guard unmetRequirement == nil else {
1193+ printSkipMessage ( testName: self . name, unmetRequirement: unmetRequirement!)
1194+ return
1195+ }
1196+
1197+ let stream = try db. watch ( )
1198+
1199+ // insert to collection to create the db
1200+ try collection. insertOne ( [ " x " : 1 ] )
1201+ let insertEvent = try stream. next ( ) ? . get ( )
1202+ expect ( insertEvent? . operationType) . to ( equal ( . insert) )
1203+
1204+ // drop the db to generate an invalidate event
1205+ try db. drop ( )
1206+ // first we see collection drop, then db drop
1207+ let dropColl = try stream. next ( ) ? . get ( )
1208+ expect ( dropColl? . operationType) . to ( equal ( . drop) )
1209+ let dropDB = try stream. next ( ) ? . get ( )
1210+ expect ( dropDB? . operationType) . to ( equal ( . dropDatabase) )
1211+
1212+ let invalidate = try stream. next ( ) ? . get ( )
1213+ expect ( invalidate? . operationType) . to ( equal ( . invalidate) )
1214+ expect ( invalidate? . ns. db) . to ( equal ( db. name) )
1215+ expect ( invalidate? . ns. collection) . to ( beNil ( ) )
1216+ }
1217+ }
11431218}
0 commit comments