Skip to content

Commit

Permalink
chore: handle socket connection errors in test
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 4, 2024
1 parent 457284c commit f19e04b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions provider/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package provider

import (
"context"
"fmt"
"math"
"net/http"
"strconv"
"sync"
"time"

"github.com/evcc-io/evcc/api"
Expand Down Expand Up @@ -86,20 +88,25 @@ func NewSocketProviderFromConfig(other map[string]interface{}) (Provider, error)
return nil, err
}

go p.listen()
errC := make(chan error, 1)
go p.run(errC)

if cc.Timeout > 0 {
select {
case <-p.val.Done():
case <-time.After(cc.Timeout):
return nil, api.ErrTimeout
case err := <-errC:
return nil, err
}
}

return p, nil
}

func (p *Socket) listen() {
func (p *Socket) run(errC chan error) {
var once sync.Once

headers := make(http.Header)
for k, v := range p.headers {
headers.Set(k, v)
Expand All @@ -115,6 +122,9 @@ func (p *Socket) listen() {
cancel()

if err != nil {
// handle initial connection error immediately
once.Do(func() { errC <- err })

p.log.ERROR.Println(err)
time.Sleep(retryDelay)
continue
Expand Down Expand Up @@ -154,6 +164,7 @@ func (p *Socket) FloatGetter() (func() (float64, error), error) {
g, err := p.StringGetter()

return func() (float64, error) {
fmt.Println("FloatGetter")
s, err := g()
if err != nil {
return 0, err
Expand Down

0 comments on commit f19e04b

Please sign in to comment.