fix: preserve partial batch property results
This commit is contained in:
+45
-6
@@ -625,26 +625,51 @@ func TestDeviceGetManyMatchesIdentityAndPreservesBusinessErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceGetManyRejectsInvalidResponseIdentities(t *testing.T) {
|
||||
func TestDeviceGetManyPreservesPartialResultsForInvalidResponseIdentities(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
response string
|
||||
wantErrs []string
|
||||
}{
|
||||
{name: "missing", response: `[{"did":"a","siid":2,"piid":1,"value":true,"code":0}]`},
|
||||
{name: "duplicate", response: `[{"did":"a","siid":2,"piid":1,"value":true,"code":0},{"did":"a","siid":2,"piid":1,"value":false,"code":0}]`},
|
||||
{name: "extra", response: `[{"did":"a","siid":2,"piid":1,"value":true,"code":0},{"did":"a","siid":2,"piid":2,"value":5,"code":0},{"did":"other","siid":9,"piid":9,"value":1,"code":0}]`},
|
||||
{name: "missing", response: `[{"did":"a","siid":2,"piid":1,"value":true,"code":0}]`, wantErrs: []string{"", "missing identity"}},
|
||||
{name: "duplicate and missing", response: `[{"did":"a","siid":2,"piid":1,"value":true,"code":0},{"did":"a","siid":2,"piid":1,"value":false,"code":0}]`, wantErrs: []string{"duplicate identity", "missing identity"}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
device := fixtureDeviceWithResults(t, []string{test.response}, 0)
|
||||
results, err := device.GetMany(context.Background(), []string{"power", "brightness"})
|
||||
if err == nil || results != nil || !strings.Contains(err.Error(), "protocol") {
|
||||
t.Fatalf("GetMany() = %#v, %v, want nil protocol error", results, err)
|
||||
if err != nil || len(results) != 2 {
|
||||
t.Fatalf("GetMany() = %#v, %v", results, err)
|
||||
}
|
||||
for index, wantErr := range test.wantErrs {
|
||||
if results[index].Name != []string{"power", "brightness"}[index] {
|
||||
t.Fatalf("result %d name = %q", index, results[index].Name)
|
||||
}
|
||||
if wantErr == "" {
|
||||
if results[index].Err != nil || results[index].Value != true {
|
||||
t.Fatalf("result %d = %#v, want successful power result", index, results[index])
|
||||
}
|
||||
} else if results[index].Err == nil || !strings.Contains(results[index].Err.Error(), wantErr) {
|
||||
t.Fatalf("result %d error = %v, want %q", index, results[index].Err, wantErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceGetManyRejectsExtraResponseIdentity(t *testing.T) {
|
||||
device := fixtureDeviceWithResults(t, []string{`[
|
||||
{"did":"a","siid":2,"piid":1,"value":true,"code":0},
|
||||
{"did":"a","siid":2,"piid":2,"value":5,"code":0},
|
||||
{"did":"other","siid":9,"piid":9,"value":1,"code":0}
|
||||
]`}, 0)
|
||||
|
||||
results, err := device.GetMany(context.Background(), []string{"power", "brightness"})
|
||||
if err == nil || results != nil || !strings.Contains(err.Error(), "protocol") {
|
||||
t.Fatalf("GetMany() = %#v, %v, want nil protocol error", results, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceGetManyValidatesBeforeNetwork(t *testing.T) {
|
||||
device, testServer := fixtureDeviceWithServer(t, nil)
|
||||
tests := [][]string{{"power", "power"}, {"power", "missing"}, {"power", "write-only"}}
|
||||
@@ -692,6 +717,20 @@ func TestDeviceGetManyWaitsOnceAfterAllChunks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceGetManyWaitsOnceWithPartialProtocolErrors(t *testing.T) {
|
||||
device := fixtureDeviceWithResults(t, []string{`[{"did":"a","siid":2,"piid":1,"value":true,"code":0}]`}, 40*time.Millisecond)
|
||||
|
||||
started := time.Now()
|
||||
results, err := device.GetMany(context.Background(), []string{"power", "brightness"})
|
||||
elapsed := time.Since(started)
|
||||
if err != nil || len(results) != 2 || results[1].Err == nil {
|
||||
t.Fatalf("GetMany() = %#v, %v", results, err)
|
||||
}
|
||||
if elapsed < 30*time.Millisecond || elapsed >= 75*time.Millisecond {
|
||||
t.Fatalf("GetMany() delay = %v, want one approximately 40ms wait", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func batchPropertyFixture(count int) (map[string]PropertySpec, []string) {
|
||||
properties := make(map[string]PropertySpec, count)
|
||||
names := make([]string, count)
|
||||
|
||||
Reference in New Issue
Block a user