address MR comments, fix race
diff --git a/apigee_sync.go b/apigee_sync.go
index 0df37d6..a4b5a16 100644
--- a/apigee_sync.go
+++ b/apigee_sync.go
@@ -304,6 +304,7 @@
log.Debug("Extracting table names from existing DB")
rows, err := db.Query("SELECT name FROM _known_tables;")
+ defer rows.Close()
if err != nil {
log.Panicf("Error reading current set of tables: %v", err)
@@ -324,19 +325,31 @@
func persistKnownTablesToDB(tables map[string]bool, db apid.DB) {
log.Debugf("Inserting table names found in snapshot into db")
- _, err := db.Exec(`CREATE TABLE _known_tables (name text, PRIMARY KEY(name));`)
+ tx, err := db.Begin()
+ if err != nil {
+ log.Panicf("Error starting transaction: %v", err)
+ }
+ defer tx.Rollback()
+
+ _, err = tx.Exec("CREATE TABLE _known_tables (name text, PRIMARY KEY(name));")
if err != nil {
log.Panicf("Could not create _known_tables table: %s", err)
}
for name := range tables {
log.Debugf("Inserting %s into _known_tables", name)
- _, err := db.Exec(`INSERT INTO _known_tables VALUES(?);`, name)
+ _, err := tx.Exec("INSERT INTO _known_tables VALUES(?);", name)
if err != nil {
log.Panicf("Error encountered inserting into known tables ", err)
}
}
+
+ err = tx.Commit()
+ if err != nil {
+ log.Panicf("Error committing transaction: %v", err)
+
+ }
}
// Skip Downloading snapshot if there is already a snapshot available from previous run
@@ -461,9 +474,10 @@
* Determine is map b is a subset of map a
*/
func mapIsSubset(a map[string]bool, b map[string]bool) bool {
-
- if b == nil {
- return true;
+
+ //nil maps should not be passed in. Making the distinction between nil map and empty map
+ if a == nil || b == nil {
+ return false;
}
for k := range b {
diff --git a/apigee_sync_test.go b/apigee_sync_test.go
index 3f8373c..12198e9 100644
--- a/apigee_sync_test.go
+++ b/apigee_sync_test.go
@@ -16,7 +16,6 @@
expectedTables["edgex.apid_cluster"] = true
expectedTables["edgex.data_scope"] = true
-
Expect(apidInfo.LastSnapshot).NotTo(BeEmpty())
apid.Events().ListenFunc(ApigeeSyncEventSelector, func(event apid.Event) {
@@ -37,6 +36,27 @@
bootstrap()
})
+ It("should correctly identify non-proper subsets with respect to maps", func() {
+
+ //test b proper subset of a
+ Expect(mapIsSubset(map[string]bool{"a": true, "b": true}, map[string]bool{"b": true})).To(BeTrue())
+
+ //test a == b
+ Expect(mapIsSubset(map[string]bool{"a": true, "b": true}, map[string]bool{"a": true, "b": true})).To(BeTrue())
+
+ //test b superset of a
+ Expect(mapIsSubset(map[string]bool{"a": true, "b": true}, map[string]bool{"a": true, "b": true, "c": true})).To(BeFalse())
+
+ //test b not subset of a
+ Expect(mapIsSubset(map[string]bool{"a": true, "b": true}, map[string]bool{"c": true})).To(BeFalse())
+
+ //test b empty
+ Expect(mapIsSubset(map[string]bool{"a": true, "b": true}, map[string]bool{})).To(BeTrue())
+
+ //test a empty
+ Expect(mapIsSubset(map[string]bool{}, map[string]bool{"b": true})).To(BeFalse())
+ })
+
// todo: disabled for now -
// there is precondition I haven't been able to track down that breaks this test on occasion
XIt("should process a new snapshot when change server requires it", func(done Done) {
diff --git a/listener_test.go b/listener_test.go
index 1d1e896..1fa2e58 100644
--- a/listener_test.go
+++ b/listener_test.go
@@ -10,11 +10,15 @@
var _ = Describe("listener", func() {
handler := handler{}
+ var saveLastSnapshot string
Context("ApigeeSync snapshot event", func() {
It("should set DB to appropriate version", func() {
+ //save the last snapshot, so we can restore it at the end of this context
+ saveLastSnapshot = apidInfo.LastSnapshot
+
event := common.Snapshot{
SnapshotInfo: "test_snapshot",
Tables: []common.Table{},
@@ -152,6 +156,9 @@
Expect(ds.CreatedBy).To(Equal("c"))
Expect(ds.Updated).To(Equal("u"))
Expect(ds.UpdatedBy).To(Equal("u"))
+
+ //restore the last snapshot
+ apidInfo.LastSnapshot = saveLastSnapshot
})
})
@@ -160,6 +167,9 @@
Context(LISTENER_TABLE_APID_CLUSTER, func() {
It("insert event should panic", func() {
+ //save the last snapshot, so we can restore it at the end of this context
+ saveLastSnapshot = apidInfo.LastSnapshot
+
event := common.ChangeList{
LastSequence: "test",
@@ -187,6 +197,8 @@
}
Expect(func() { handler.Handle(&event) }).To(Panic())
+ //restore the last snapshot
+ apidInfo.LastSnapshot = saveLastSnapshot
})
PIt("delete event should kill all the things!")
@@ -195,6 +207,10 @@
Context(LISTENER_TABLE_DATA_SCOPE, func() {
It("insert event should add", func() {
+ //save the last snapshot, so we can restore it at the end of this context
+ saveLastSnapshot = apidInfo.LastSnapshot
+
+
event := common.ChangeList{
LastSequence: "test",
Changes: []common.Change{
@@ -306,8 +322,12 @@
}
Expect(func() { handler.Handle(&event) }).To(Panic())
+ //restore the last snapshot
+ apidInfo.LastSnapshot = saveLastSnapshot
})
+
+
})
})