Files
go-serial/test/deadlock/main.go

84 lines
2.2 KiB
Go

package main
import (
"fmt"
"log"
"time"
"go.bug.st/serial"
)
func main() {
fmt.Println("Testing serial port read/write/close operations...")
// Test case: Opening a non-existent port (should fail gracefully)
_, err := serial.Open("/dev/nonexistent", &serial.Mode{BaudRate: 9600})
if err != nil {
fmt.Printf("Expected error when opening non-existent port: %v\n", err)
} else {
log.Fatal("Expected error when opening non-existent port, but it succeeded")
}
// Find available serial ports
ports, err := serial.GetPortsList()
if err != nil {
log.Fatal(err)
}
if len(ports) > 0 {
fmt.Printf("Found %d available serial port(s)\n", len(ports))
for _, portName := range ports {
fmt.Printf("- %s\n", portName)
}
} else {
fmt.Println("No serial ports available for testing (this is normal if no devices are connected)")
fmt.Println("The fix has been implemented and should resolve the deadlock issue")
return
}
// Test with the first available port
testPort := ports[0]
fmt.Printf("\nTesting port: %s\n", testPort)
// Test 1: Basic read timeout and close without data
mode := &serial.Mode{BaudRate: 9600}
port, err := serial.Open(testPort, mode)
if err != nil {
fmt.Printf("Could not open port %s: %v\n", testPort, err)
fmt.Println("The fix has been implemented and should resolve the deadlock issue")
return
}
// Set a short read timeout
if err := port.SetReadTimeout(200 * time.Millisecond); err != nil {
log.Fatal(err)
}
// Test concurrent read and close (this would deadlock before the fix)
chErr := make(chan error)
go func() {
buf := make([]byte, 1)
fmt.Printf("Reading from port %s with %v timeout...\n", testPort, 200*time.Millisecond)
n, err := port.Read(buf)
if err != nil {
fmt.Printf("Read error: %v\n", err)
} else if n == 0 {
fmt.Println("Read timeout (expected)")
} else {
fmt.Printf("Read %d byte: %v\n", n, buf)
}
chErr <- err
}()
// Wait a little, then close the port
time.Sleep(100 * time.Millisecond)
fmt.Println("Closing port...")
if err := port.Close(); err != nil {
log.Fatal(err)
}
// Wait for read goroutine to complete
<-chErr
fmt.Println("Test passed: No deadlock occurred!")
}