Skip to content

Commit ac70a02

Browse files
committed
[bugfix] Fix unit test
1 parent 8e046d1 commit ac70a02

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

internal/app/controller/publisher.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ const maxDatagramSize = 8192
1313
// Publisher can publish state and commands to the teams
1414
type Publisher struct {
1515
conn *net.UDPConn
16-
message refproto.Referee
16+
message RefMessage
17+
}
18+
19+
type RefMessage struct {
20+
referee refproto.Referee
21+
send func()
1722
}
1823

1924
// NewPublisher creates a new publisher that publishes referee messages via UDP to the teams
@@ -34,8 +39,9 @@ func NewPublisher(address string) (publisher Publisher, err error) {
3439
log.Println("Publishing to", address)
3540

3641
publisher.conn = conn
42+
publisher.message = RefMessage{send: publisher.send}
3743

38-
initRefereeMessage(&publisher.message)
44+
initRefereeMessage(&publisher.message.referee)
3945

4046
return
4147
}
@@ -69,17 +75,21 @@ func initTeamInfo(t *refproto.Referee_TeamInfo) {
6975

7076
// Publish the state and command
7177
func (p *Publisher) Publish(state *State) {
78+
p.message.Publish(state)
79+
}
7280

73-
if p.conn == nil {
74-
return
75-
}
76-
81+
// Publish the state and command
82+
func (p *RefMessage) Publish(state *State) {
7783
p.setState(state)
7884
p.sendCommands(state)
7985
}
8086

8187
func (p *Publisher) send() {
82-
bytes, err := proto.Marshal(&p.message)
88+
if p.conn == nil {
89+
return
90+
}
91+
92+
bytes, err := proto.Marshal(&p.message.referee)
8393
if err != nil {
8494
log.Printf("Could not marshal referee message: %v\nError: %v", p.message, err)
8595
return
@@ -90,8 +100,8 @@ func (p *Publisher) send() {
90100
}
91101
}
92102

93-
func (p *Publisher) setState(state *State) (republish bool) {
94-
r := &p.message
103+
func (p *RefMessage) setState(state *State) (republish bool) {
104+
r := &p.referee
95105

96106
r.GameEvents = mapGameEvents(state.GameEvents)
97107
r.DesignatedPosition = mapLocation(state.PlacementPos)
@@ -105,26 +115,26 @@ func (p *Publisher) setState(state *State) (republish bool) {
105115
return
106116
}
107117

108-
func (p *Publisher) sendCommands(state *State) {
118+
func (p *RefMessage) sendCommands(state *State) {
109119
newCommand := mapCommand(state.Command, state.CommandFor)
110120

111121
// send the GOAL command based on the team score for compatibility with old behavior
112-
if state.TeamState[TeamYellow].Goals > int(*p.message.Yellow.Score) {
122+
if state.TeamState[TeamYellow].Goals > int(*p.referee.Yellow.Score) {
113123
p.updateCommand(refproto.Referee_GOAL_YELLOW)
114124
p.send()
115125
p.updateCommand(newCommand)
116-
} else if state.TeamState[TeamBlue].Goals > int(*p.message.Blue.Score) {
126+
} else if state.TeamState[TeamBlue].Goals > int(*p.referee.Blue.Score) {
117127
p.updateCommand(refproto.Referee_GOAL_BLUE)
118128
p.send()
119129
p.updateCommand(newCommand)
120-
} else if *p.message.Command != newCommand {
130+
} else if *p.referee.Command != newCommand {
121131
switch state.Command {
122132
case CommandBallPlacement,
123133
CommandDirect,
124134
CommandIndirect,
125135
CommandKickoff,
126136
CommandPenalty:
127-
if *p.message.Command != refproto.Referee_STOP {
137+
if *p.referee.Command != refproto.Referee_STOP {
128138
// send a STOP right before the actual command to be compatible with old behavior
129139
p.updateCommand(refproto.Referee_STOP)
130140
p.send()
@@ -136,6 +146,12 @@ func (p *Publisher) sendCommands(state *State) {
136146
p.send()
137147
}
138148

149+
func (p *RefMessage) updateCommand(newCommand refproto.Referee_Command) {
150+
*p.referee.Command = newCommand
151+
*p.referee.CommandCounter++
152+
*p.referee.CommandTimestamp = uint64(time.Now().UnixNano() / 1000)
153+
}
154+
139155
func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {
140156
mappedEvents := make([]*refproto.GameEvent, len(events))
141157
for i, e := range events {
@@ -144,12 +160,6 @@ func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {
144160
return mappedEvents
145161
}
146162

147-
func (p *Publisher) updateCommand(newCommand refproto.Referee_Command) {
148-
*p.message.Command = newCommand
149-
*p.message.CommandCounter++
150-
*p.message.CommandTimestamp = uint64(time.Now().UnixNano() / 1000)
151-
}
152-
153163
func mapCommand(command RefCommand, team Team) refproto.Referee_Command {
154164
switch command {
155165
case CommandHalt:

internal/app/controller/publisher_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
)
1010

1111
func Test_updateMessage(t *testing.T) {
12-
referee := refproto.Referee{}
13-
initRefereeMessage(&referee)
1412
state := NewState()
1513

16-
updateMessage(&referee, state)
14+
r := &RefMessage{send: func() {}}
15+
referee := &r.referee
16+
initRefereeMessage(referee)
17+
r.Publish(state)
1718

1819
if *referee.PacketTimestamp <= 0 {
1920
t.Errorf("Wrong packet timestamp: %v", *referee.PacketTimestamp)

0 commit comments

Comments
 (0)