Skip to content

Added (b *bus) HasSubscriber() for checking if a bus has any subscriber #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 18 additions & 8 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
type Bus struct {
rwc ReadWriteCloser

handler []Handler
handler []Handler
disconnect chan bool
}

// NewBusForInterfaceWithName returns a bus from the network interface with name ifaceName.
Expand All @@ -32,25 +33,30 @@ func NewBusForInterfaceWithName(ifaceName string) (*Bus, error) {
// NewBus returns a new CAN bus.
func NewBus(rwc ReadWriteCloser) *Bus {
return &Bus{
rwc: rwc,
handler: make([]Handler, 0),
rwc: rwc,
handler: make([]Handler, 0),
disconnect: make(chan bool, 1),
}
}

// ConnectAndPublish starts handling CAN frames to publish them to handlers.
func (b *Bus) ConnectAndPublish() error {
for {
err := b.publishNextFrame()
if err != nil {
return err
select {
case <-b.disconnect:
return nil
default:
err := b.publishNextFrame()
if err != nil {
return err
}
}
}

return nil
}

// Disconnect stops handling CAN frames.
func (b *Bus) Disconnect() error {
b.disconnect <- true
return b.rwc.Close()
}

Expand All @@ -75,6 +81,10 @@ func (b *Bus) Unsubscribe(handler Handler) {
}
}

func (b *Bus) HasSubscriber() bool {
return len(b.handler) > 0
}

// Publish publishes a frame on the bus.
//
// Frames publishes with the Publish methods are not received by handlers.
Expand Down
63 changes: 63 additions & 0 deletions bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,66 @@ func TestUnsubscribe(t *testing.T) {
t.Fatal(x)
}
}

func TestHasHandler(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)

if bus.HasSubscriber() != false {
t.Fatal("bus should not have subscriber on creation")
}

handler := newTestHandler()
bus.Subscribe(handler)
if bus.HasSubscriber() != true {
t.Fatal("bus should have subscriber after subscription")
}

bus.Unsubscribe(handler)
if bus.HasSubscriber() != false {
t.Fatal("bus should not have subscriber after subscriber unsubscribed")
}
}

func TestDisconnect(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)

connectAndPublishReturns := make(chan bool)

go func() {
bus.ConnectAndPublish()
connectAndPublishReturns <- true
}()
//Should make ConnectAndPublish return
bus.Disconnect()
// if it doesn't we're stuck on 1
timeout := time.After(100 * time.Millisecond)
select {
case <-timeout:
t.Fatal("bus.Disconnect does not abort ConnectAndPublish loop")
case <-connectAndPublishReturns:
//Success
return
}
}

func TestDisconnectWhenConnectAndPublishedNotCalled(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)

disconnected := make(chan bool)

go func() {
bus.Disconnect()
disconnected <- true
}()

timeout := time.After(100 * time.Millisecond)
select {
case <-timeout:
t.Fatal("bus.Disconnect() blocks when bus.ConnectAndPublish() not called")
case <-disconnected:
return
}
}
3 changes: 2 additions & 1 deletion cmd/candump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package main
import (
"flag"
"fmt"
"github.com/brutella/can"
"log"
"net"
"os"
"os/signal"

"github.com/dockstavarvet/can"
)

var i = flag.String("if", "", "network interface name")
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/brutella/can
module github.com/dockstavarvet/can

go 1.15

require golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
github.com/dockstavarvet/can v0.0.4/go.mod h1:jpF3R1Y7uEyvJmdl9Jo5bs2jp7+EB+AnrCrfHQmmJm4=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 h1:0oC8rFnE+74kEmuHZ46F6KHsMr5Gx2gUQPuNz28iQZM=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=