MCPcopy Create free account
hub / github.com/evcc-io/evcc / TestConcurrentRead

Function TestConcurrentRead

server/modbus/proxy_test.go:18–57  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestConcurrentRead(t *testing.T) {
19 l, err := net.Listen("tcp", "localhost:0")
20 require.NoError(t, err)
21 defer l.Close()
22
23 srv, _ := mbserver.New(&echoHandler{
24 id: 0,
25 RequestHandler: new(mbserver.DummyHandler),
26 })
27 require.NoError(t, srv.Start(l))
28 defer func() { _ = srv.Stop() }()
29
30 var wg sync.WaitGroup
31
32 for id := 1; id <= 10; id++ {
33 wg.Go(func() {
34 // client
35 conn, err := modbus.NewConnection(t.Context(), l.Addr().String(), "", "", 0, modbus.Tcp, uint8(id))
36 require.NoError(t, err)
37
38 for range 50 {
39 addr := uint16(rand.Int31n(200) + 1)
40 qty := uint16(rand.Int31n(32) + 1)
41
42 b, err := conn.ReadInputRegisters(addr, qty)
43 require.NoError(t, err)
44
45 if err == nil {
46 for u := range qty {
47 assert.Equal(t, addr^uint16(id)^u, binary.BigEndian.Uint16(b[2*u:]))
48 }
49 }
50
51 time.Sleep(time.Duration(rand.Int31n(1000)) * time.Microsecond)
52 }
53 })
54 }
55
56 wg.Wait()
57}
58
59func TestReadCoils(t *testing.T) {
60 // downstream server

Callers

nothing calls this directly

Calls 11

ReadInputRegistersMethod · 0.95
NewConnectionFunction · 0.92
StartMethod · 0.80
StopMethod · 0.80
DurationMethod · 0.80
WaitMethod · 0.80
NewMethod · 0.65
StringMethod · 0.65
ListenMethod · 0.45
CloseMethod · 0.45
AddrMethod · 0.45

Tested by

no test coverage detected