diff options
Diffstat (limited to 'src/pkg/testing/script/script.go')
-rw-r--r-- | src/pkg/testing/script/script.go | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/src/pkg/testing/script/script.go b/src/pkg/testing/script/script.go index 623685efd..9a41a467f 100644 --- a/src/pkg/testing/script/script.go +++ b/src/pkg/testing/script/script.go @@ -6,37 +6,37 @@ package script import ( - "fmt"; - "os"; - "rand"; - "reflect"; - "strings"; + "fmt" + "os" + "rand" + "reflect" + "strings" ) // An Event is an element in a partially ordered set that either sends a value // to a channel or expects a value from a channel. type Event struct { - name string; - occurred bool; - predecessors []*Event; - action action; + name string + occurred bool + predecessors []*Event + action action } type action interface { // getSend returns nil if the action is not a send action. - getSend() sendAction; + getSend() sendAction // getRecv returns nil if the action is not a receive action. - getRecv() recvAction; + getRecv() recvAction // getChannel returns the channel that the action operates on. - getChannel() interface{}; + getChannel() interface{} } type recvAction interface { - recvMatch(interface{}) bool; + recvMatch(interface{}) bool } type sendAction interface { - send(); + send() } // isReady returns true if all the predecessors of an Event have occurred. @@ -47,87 +47,87 @@ func (e Event) isReady() bool { } } - return true; + return true } // A Recv action reads a value from a channel and uses reflect.DeepMatch to // compare it with an expected value. type Recv struct { - Channel interface{}; - Expected interface{}; + Channel interface{} + Expected interface{} } -func (r Recv) getRecv() recvAction { return r } +func (r Recv) getRecv() recvAction { return r } -func (Recv) getSend() sendAction { return nil } +func (Recv) getSend() sendAction { return nil } -func (r Recv) getChannel() interface{} { return r.Channel } +func (r Recv) getChannel() interface{} { return r.Channel } func (r Recv) recvMatch(chanEvent interface{}) bool { - c, ok := chanEvent.(channelRecv); + c, ok := chanEvent.(channelRecv) if !ok || c.channel != r.Channel { return false } - return reflect.DeepEqual(c.value, r.Expected); + return reflect.DeepEqual(c.value, r.Expected) } // A RecvMatch action reads a value from a channel and calls a function to // determine if the value matches. type RecvMatch struct { - Channel interface{}; - Match func(interface{}) bool; + Channel interface{} + Match func(interface{}) bool } -func (r RecvMatch) getRecv() recvAction { return r } +func (r RecvMatch) getRecv() recvAction { return r } -func (RecvMatch) getSend() sendAction { return nil } +func (RecvMatch) getSend() sendAction { return nil } -func (r RecvMatch) getChannel() interface{} { return r.Channel } +func (r RecvMatch) getChannel() interface{} { return r.Channel } func (r RecvMatch) recvMatch(chanEvent interface{}) bool { - c, ok := chanEvent.(channelRecv); + c, ok := chanEvent.(channelRecv) if !ok || c.channel != r.Channel { return false } - return r.Match(c.value); + return r.Match(c.value) } // A Closed action matches if the given channel is closed. The closing is // treated as an event, not a state, thus Closed will only match once for a // given channel. type Closed struct { - Channel interface{}; + Channel interface{} } -func (r Closed) getRecv() recvAction { return r } +func (r Closed) getRecv() recvAction { return r } -func (Closed) getSend() sendAction { return nil } +func (Closed) getSend() sendAction { return nil } -func (r Closed) getChannel() interface{} { return r.Channel } +func (r Closed) getChannel() interface{} { return r.Channel } func (r Closed) recvMatch(chanEvent interface{}) bool { - c, ok := chanEvent.(channelClosed); + c, ok := chanEvent.(channelClosed) if !ok || c.channel != r.Channel { return false } - return true; + return true } // A Send action sends a value to a channel. The value must match the // type of the channel exactly unless the channel if of type chan interface{}. type Send struct { - Channel interface{}; - Value interface{}; + Channel interface{} + Value interface{} } -func (Send) getRecv() recvAction { return nil } +func (Send) getRecv() recvAction { return nil } -func (s Send) getSend() sendAction { return s } +func (s Send) getSend() sendAction { return s } -func (s Send) getChannel() interface{} { return s.Channel } +func (s Send) getChannel() interface{} { return s.Channel } func newEmptyInterface(args ...) reflect.Value { return reflect.NewValue(args).(*reflect.StructValue).Field(0) @@ -137,53 +137,53 @@ func (s Send) send() { // With reflect.ChanValue.Send, we must match the types exactly. So, if // s.Channel is a chan interface{} we convert s.Value to an interface{} // first. - c := reflect.NewValue(s.Channel).(*reflect.ChanValue); - var v reflect.Value; + c := reflect.NewValue(s.Channel).(*reflect.ChanValue) + var v reflect.Value if iface, ok := c.Type().(*reflect.ChanType).Elem().(*reflect.InterfaceType); ok && iface.NumMethod() == 0 { v = newEmptyInterface(s.Value) } else { v = reflect.NewValue(s.Value) } - c.Send(v); + c.Send(v) } // A Close action closes the given channel. type Close struct { - Channel interface{}; + Channel interface{} } -func (Close) getRecv() recvAction { return nil } +func (Close) getRecv() recvAction { return nil } -func (s Close) getSend() sendAction { return s } +func (s Close) getSend() sendAction { return s } -func (s Close) getChannel() interface{} { return s.Channel } +func (s Close) getChannel() interface{} { return s.Channel } -func (s Close) send() { reflect.NewValue(s.Channel).(*reflect.ChanValue).Close() } +func (s Close) send() { reflect.NewValue(s.Channel).(*reflect.ChanValue).Close() } // A ReceivedUnexpected error results if no active Events match a value // received from a channel. type ReceivedUnexpected struct { - Value interface{}; - ready []*Event; + Value interface{} + ready []*Event } func (r ReceivedUnexpected) String() string { - names := make([]string, len(r.ready)); + names := make([]string, len(r.ready)) for i, v := range r.ready { names[i] = v.name } - return fmt.Sprintf("received unexpected value on one of the channels: %#v. Runnable events: %s", r.Value, strings.Join(names, ", ")); + return fmt.Sprintf("received unexpected value on one of the channels: %#v. Runnable events: %s", r.Value, strings.Join(names, ", ")) } // A SetupError results if there is a error with the configuration of a set of // Events. type SetupError string -func (s SetupError) String() string { return string(s) } +func (s SetupError) String() string { return string(s) } func NewEvent(name string, predecessors []*Event, action action) *Event { - e := &Event{name, false, predecessors, action}; - return e; + e := &Event{name, false, predecessors, action} + return e } // Given a set of Events, Perform repeatedly iterates over the set and finds the @@ -220,20 +220,20 @@ func NewEvent(name string, predecessors []*Event, action action) *Event { // thus Perform may see a value from a channel that is not in the current ready // set and fail. func Perform(seed int64, events []*Event) (err os.Error) { - r := rand.New(rand.NewSource(seed)); + r := rand.New(rand.NewSource(seed)) - channels, err := getChannels(events); + channels, err := getChannels(events) if err != nil { return } - multiplex := make(chan interface{}); + multiplex := make(chan interface{}) for _, channel := range channels { go recvValues(multiplex, channel) } Outer: for { - ready, err := readyEvents(events); + ready, err := readyEvents(events) if err != nil { return err } @@ -243,113 +243,113 @@ Outer: break } - event := ready[r.Intn(len(ready))]; + event := ready[r.Intn(len(ready))] if send := event.action.getSend(); send != nil { - send.send(); - event.occurred = true; - continue; + send.send() + event.occurred = true + continue } - v := <-multiplex; + v := <-multiplex for _, event := range ready { if recv := event.action.getRecv(); recv != nil && recv.recvMatch(v) { - event.occurred = true; - continue Outer; + event.occurred = true + continue Outer } } - return ReceivedUnexpected{v, ready}; + return ReceivedUnexpected{v, ready} } - return nil; + return nil } // getChannels returns all the channels listed in any receive events. func getChannels(events []*Event) ([]interface{}, os.Error) { - channels := make([]interface{}, len(events)); + channels := make([]interface{}, len(events)) - j := 0; + j := 0 for _, event := range events { if recv := event.action.getRecv(); recv == nil { continue } - c := event.action.getChannel(); + c := event.action.getChannel() if _, ok := reflect.NewValue(c).(*reflect.ChanValue); !ok { return nil, SetupError("one of the channel values is not a channel") } - duplicate := false; + duplicate := false for _, other := range channels[0:j] { if c == other { - duplicate = true; - break; + duplicate = true + break } } if !duplicate { - channels[j] = c; - j++; + channels[j] = c + j++ } } - return channels[0:j], nil; + return channels[0:j], nil } // recvValues is a multiplexing helper function. It reads values from the given // channel repeatedly, wrapping them up as either a channelRecv or // channelClosed structure, and forwards them to the multiplex channel. func recvValues(multiplex chan<- interface{}, channel interface{}) { - c := reflect.NewValue(channel).(*reflect.ChanValue); + c := reflect.NewValue(channel).(*reflect.ChanValue) for { - v := c.Recv(); + v := c.Recv() if c.Closed() { - multiplex <- channelClosed{channel}; - return; + multiplex <- channelClosed{channel} + return } - multiplex <- channelRecv{channel, v.Interface()}; + multiplex <- channelRecv{channel, v.Interface()} } } type channelClosed struct { - channel interface{}; + channel interface{} } type channelRecv struct { - channel interface{}; - value interface{}; + channel interface{} + value interface{} } // readyEvents returns the subset of events that are ready. func readyEvents(events []*Event) ([]*Event, os.Error) { - ready := make([]*Event, len(events)); + ready := make([]*Event, len(events)) - j := 0; - eventsWaiting := false; + j := 0 + eventsWaiting := false for _, event := range events { if event.occurred { continue } - eventsWaiting = true; + eventsWaiting = true if event.isReady() { - ready[j] = event; - j++; + ready[j] = event + j++ } } if j == 0 && eventsWaiting { - names := make([]string, len(events)); + names := make([]string, len(events)) for _, event := range events { if event.occurred { continue } - names[j] = event.name; + names[j] = event.name } - return nil, SetupError("dependency cycle in events. These events are waiting to run but cannot: " + strings.Join(names, ", ")); + return nil, SetupError("dependency cycle in events. These events are waiting to run but cannot: " + strings.Join(names, ", ")) } - return ready[0:j], nil; + return ready[0:j], nil } |