From 5c47a410cfaf6a97f839a8da1af92279eaff61eb Mon Sep 17 00:00:00 2001 From: Darell Tan Date: Thu, 27 Apr 2023 02:14:53 +0800 Subject: [PATCH] Added test for FsStore values clobbering It was fixed upstream during a rewrite on functions in FsStore. See my comments on brutella/hap#28. --- bridge_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 bridge_test.go diff --git a/bridge_test.go b/bridge_test.go new file mode 100644 index 0000000..09067fe --- /dev/null +++ b/bridge_test.go @@ -0,0 +1,87 @@ +package hapz2m + +import ( + "context" + "fmt" + "os" + "sync" + "testing" +) + +const ContactSensorTemplate = ` + { + "network_address": %[1]d, + "friendly_name": "0x00158d00aabbcc%02[1]d", + "ieee_address": "0x00158d00aabbcc%02[1]d", + "interview_completed": true, + "interviewing": false, + "disabled": false, + "supported": true, + "definition": { + "exposes": [ + { + "access": 1, + "name": "contact", + "property": "contact", + "type": "binary", + "value_off": "A", + "value_on": "B" + } + ] + } + }` + +func TestBridgePersistState(t *testing.T) { + dir, err := os.MkdirTemp("", "hapz2m-bridge*") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + var m sync.Map + ctx := context.Background() + + b := NewBridge(ctx, dir) + + // devices + s1 := fmt.Appendf(nil, ContactSensorTemplate, 10) + s2 := fmt.Appendf(nil, ContactSensorTemplate, 20) + + err = b.AddDevicesFromJSON(fmt.Appendf(nil, "[%s, %s]", s1, s2)) + if err != nil { + t.Fatalf("cannot add devices: %v", err) + } + + // empty state, should have no errors + t.Logf("loading from empty db") + if err := b.loadZ2MState(&m); err != nil { + t.Errorf("empty state load should not error: %v", err) + } + + // save 2 devices + t.Logf("persisting state") + if err := b.saveZ2MState(); err != nil { + t.Errorf("can't persist state: %v", err) + } + + // re-create with less devices + b2 := NewBridge(ctx, dir) + b2.AddDevicesFromJSON(fmt.Appendf(nil, "[%s]", s1)) + + t.Logf("loading from initial db") + if err := b2.loadZ2MState(&m); err != nil { + t.Errorf("load err: %v", err) + } + + t.Logf("persisting state") + if err := b2.saveZ2MState(); err != nil { + t.Errorf("can't persist state: %v", err) + } + + // reload from smaller-sized state + t.Logf("re-loading from db") + if err := b2.loadZ2MState(&m); err != nil { + t.Errorf("load err: %v", err) + } + +} diff --git a/go.mod b/go.mod index e9adf66..2a67647 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module hapz2m go 1.20 require ( - github.com/brutella/hap v0.0.26-0.20230413145315-4d1fbc179bbb + github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4 github.com/eclipse/paho.mqtt.golang v1.4.2 ) diff --git a/go.sum b/go.sum index 6d40869..9d4fdca 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/brutella/dnssd v1.2.6 h1:/0P13JkHLRzeLQkWRPEn4hJCr4T3NfknIFw3aNPIC34= github.com/brutella/dnssd v1.2.6/go.mod h1:JoW2sJUrmVIef25G6lrLj7HS6Xdwh6q8WUIvMkkBYXs= github.com/brutella/hap v0.0.26-0.20230413145315-4d1fbc179bbb h1:Pfh+FPHinTvHjGjvf3xW+IrJl+FL1tAZOlW2l2sWQ2g= github.com/brutella/hap v0.0.26-0.20230413145315-4d1fbc179bbb/go.mod h1:jq5zNqV0/sUSWnYQ9hBUDDXGfBiZJm1TtnptNw02PfI= +github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4 h1:9PsfodvGUvuN15W6lkaeJOl1pegGNcgcgp3F0kRWS5M= +github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4/go.mod h1:jq5zNqV0/sUSWnYQ9hBUDDXGfBiZJm1TtnptNw02PfI= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=