48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package nut
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNUTRecoversFromStaleData(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
responses := []string{
|
|
"BEGIN LIST VAR h1000m\nVAR h1000m ups.status \"OL\"\nEND LIST VAR h1000m\n",
|
|
"ERR DATA-STALE\n",
|
|
"BEGIN LIST VAR h1000m\nVAR h1000m ups.status \"OB LB\"\nEND LIST VAR h1000m\n",
|
|
}
|
|
go func() {
|
|
for _, response := range responses {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
conn.SetDeadline(time.Now().Add(time.Second))
|
|
command, _ := bufio.NewReader(conn).ReadString('\n')
|
|
if command == "LIST VAR h1000m\n" {
|
|
fmt.Fprint(conn, response)
|
|
}
|
|
conn.Close()
|
|
}
|
|
}()
|
|
address := listener.Addr().String()
|
|
for _, expected := range []string{"OL", "NUT: ERR DATA-STALE", "OB LB"} {
|
|
values, err := ReadUPS(address, "h1000m", time.Second)
|
|
actual := values["ups.status"]
|
|
if err != nil {
|
|
actual = err.Error()
|
|
}
|
|
if actual != expected {
|
|
t.Fatalf("got %q, want %q", actual, expected)
|
|
}
|
|
}
|
|
}
|