Added test for FsStore values clobbering

It was fixed upstream during a rewrite on functions in FsStore.
See my comments on brutella/hap#28.
This commit is contained in:
Darell Tan
2023-04-27 02:14:53 +08:00
parent 84398fc53a
commit 5c47a410cf
3 changed files with 90 additions and 1 deletions

87
bridge_test.go Normal file
View File

@@ -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)
}
}

2
go.mod
View File

@@ -3,7 +3,7 @@ module hapz2m
go 1.20 go 1.20
require ( 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 github.com/eclipse/paho.mqtt.golang v1.4.2
) )

2
go.sum
View File

@@ -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/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 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.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 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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= github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=