Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions internal/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,37 @@ func GenerateSimulatedWeights() []float64 {
return weights
}

// Port interface to abstract serial port dependency for testing
type Port interface {
io.ReadWriteCloser
SetReadTimeout(time.Duration) error
}

// variable to allow mocking serial.Open
var serialOpen = func(name string, mode *serial.Mode) (Port, error) {
return serial.Open(name, mode)
}

// Reader manages serial port communication with the scale
type Reader struct {
config *config.Config
broadcast chan<- string
port serial.Port
port Port
mu sync.Mutex
stopCh chan struct{}
}

func (r *Reader) sleep(ctx context.Context, d time.Duration) bool {
select {
case <-ctx.Done():
return false
case <-r.stopCh:
return false
case <-time.After(d):
return true
}
}

// NewReader creates a new scale reader
func NewReader(cfg *config.Config, broadcast chan<- string) *Reader {
return &Reader{
Expand Down Expand Up @@ -148,9 +170,11 @@ func (r *Reader) readCycle(ctx context.Context) {
return
case r.broadcast <- fmt.Sprintf("%.2f", peso):
}
time.Sleep(300 * time.Millisecond)
if !r.sleep(ctx, 300*time.Millisecond) {
return
}
}
time.Sleep(RetryDelay)
r.sleep(ctx, RetryDelay)
return
}

Expand All @@ -159,7 +183,7 @@ func (r *Reader) readCycle(ctx context.Context) {
log.Printf("[X] No se pudo abrir el puerto serial %s: %v. Reintentando en %s...",
conf.Puerto, err, RetryDelay)
r.sendError(ErrConnection) // Notify clients of connection failure
time.Sleep(RetryDelay)
r.sleep(ctx, RetryDelay)
return
}

Expand Down Expand Up @@ -194,11 +218,21 @@ func (r *Reader) readCycle(ctx context.Context) {
}
r.port = nil
r.mu.Unlock()
time.Sleep(RetryDelay)
r.sleep(ctx, RetryDelay)
break
}

time.Sleep(500 * time.Millisecond)
r.mu.Unlock()

if !r.sleep(ctx, 500*time.Millisecond) {
return
}

r.mu.Lock()
if r.port == nil {
r.mu.Unlock()
break
}

// Read response
buf := make([]byte, 20)
Expand All @@ -218,7 +252,7 @@ func (r *Reader) readCycle(ctx context.Context) {
log.Printf("[!] %s: %s - %v", ErrorDescriptions[ErrRead], conf.Puerto, err)
r.sendError(ErrRead)
r.closePort()
time.Sleep(RetryDelay)
r.sleep(ctx, RetryDelay)
}
continue
}
Expand All @@ -235,11 +269,13 @@ func (r *Reader) readCycle(ctx context.Context) {
log.Println("[!] No se recibió peso significativo.")
}

time.Sleep(300 * time.Millisecond)
if !r.sleep(ctx, 300*time.Millisecond) {
return
}
}

log.Printf("[~] Esperando %s antes de intentar reconectar al puerto serial...", RetryDelay)
time.Sleep(RetryDelay)
r.sleep(ctx, RetryDelay)
}

func (r *Reader) sendError(code string) {
Expand All @@ -256,7 +292,7 @@ func (r *Reader) connect(puerto string) error {
r.mu.Lock()
defer r.mu.Unlock()

port, err := serial.Open(puerto, mode)
port, err := serialOpen(puerto, mode)
if err != nil {
return err
}
Expand Down
105 changes: 105 additions & 0 deletions internal/scale/scale_perf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package scale

import (
"context"
"io"
"sync"
"testing"
"time"

"github.com/adcondev/scale-daemon/internal/config"
"go.bug.st/serial"
)

// MockPort implements Port interface for testing
type MockPort struct {
mu sync.Mutex
closed bool
}

func (m *MockPort) Read(p []byte) (n int, err error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.closed {
return 0, io.EOF
}
// Simulate some data
return copy(p, []byte("10.50")), nil
}

func (m *MockPort) Write(p []byte) (n int, err error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.closed {
return 0, io.ErrClosedPipe
}
return len(p), nil
}

func (m *MockPort) Close() error {
m.mu.Lock()
defer m.mu.Unlock()
m.closed = true
return nil
}

func (m *MockPort) SetReadTimeout(_ time.Duration) error {
return nil
}

func TestLockContention(t *testing.T) {
// Setup config
cfg := config.New(config.Environment{
DefaultPort: "COM_TEST",
DefaultMode: false, // Ensure we use the real connection path
})

// Override serialOpen for testing
origSerialOpen := serialOpen
defer func() { serialOpen = origSerialOpen }()

mockPort := &MockPort{}
serialOpen = func(_ string, _ *serial.Mode) (Port, error) {
return mockPort, nil
}

broadcast := make(chan string, 10)
r := NewReader(cfg, broadcast)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var wg sync.WaitGroup
wg.Add(1)
// Start reader in background
go func() {
defer wg.Done()
r.Start(ctx)
}()

// Wait for the reader to enter the read loop and acquire the lock.
// We want to catch it during the 500ms sleep.
// Since we don't know exactly when it starts sleeping, we can try multiple times or just wait a bit.
// Connect happens fast (mock). Write happens fast (mock).
// So sleep starts almost immediately.
time.Sleep(50 * time.Millisecond)

// Now try to close port. This acquires the lock.
start := time.Now()

// r.ClosePort() will block until the lock is released.
// If the lock is held during sleep, this will take ~450ms (500ms - 50ms).
r.ClosePort()

duration := time.Since(start)

t.Logf("ClosePort duration: %v", duration)

if duration > 100*time.Millisecond {
t.Errorf("Lock contention detected: ClosePort took %v, expected < 100ms. The lock is likely held during sleep.", duration)
}

// Wait for the goroutine to finish
cancel()
wg.Wait()
}
25 changes: 0 additions & 25 deletions internal/scale/scale_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scale

import (
"math"
"testing"
"time"
)
Expand Down Expand Up @@ -68,27 +67,3 @@ func TestSendError(t *testing.T) {
t.Error("sendError blocked when channel was full")
}
}

func TestGenerateSimulatedWeights(t *testing.T) {
weights := GenerateSimulatedWeights()

// Check length
if len(weights) != 6 {
t.Errorf("Expected 6 weights, got %d", len(weights))
}

// Check values
for i, w := range weights {
// Check range
if w < 0.95 || w > 30.05 {
t.Errorf("Weight %d out of range (0.95-30.05): %f", i, w)
}

// Check decimal places (should be at most 2)
// We multiply by 100 and check if it's close to an integer
scaled := w * 100
if math.Abs(scaled-math.Round(scaled)) > 1e-9 {
t.Errorf("Weight %d has more than 2 decimal places: %f (scaled: %f)", i, w, scaled)
}
}
}
43 changes: 0 additions & 43 deletions internal/server/server_test.go

This file was deleted.