PkgGoDev Go Report Card


Please refer to project README or GoDoc page for more informations.



    Package mock implements a mocked lifxlan device listening on localhost, which can be used in test code to test API calls.

    Please refer to examples to see how to use them in the test code.

    Example (TestGetLabel)

      This example demonstrates how to mock a response in test code.

      Example (TestGetLabelWithHandlerFunc)

        This example demonstrates how to mock a response with custom HandlerFunc.

        Example (TestNotEnoughAcks)

          This example demonstrates how to mock a not enough acks situation in test code.





          View Source
          const ListenAddr = ""

            ListenAddr is the addr to listen on this device.

            View Source
            const Target lifxlan.Target = 1

              Target is the mocked device target.


              This section is empty.


              func DefaultHandlerFunc

              func DefaultHandlerFunc(
              	s *Service,
              	conn net.PacketConn,
              	addr net.Addr,
              	orig *lifxlan.Response,

                DefaultHandlerFunc is the default HandlerFunc to be used when it's not in the Handlers map.


                type HandlerFunc

                type HandlerFunc func(s *Service, conn net.PacketConn, addr net.Addr, orig *lifxlan.Response)

                  HandlerFunc defines the handler function.

                  func StateUnhandledHandler

                  func StateUnhandledHandler(msg lifxlan.MessageType) HandlerFunc

                    StateUnhandledHandler generates a HandlerFunc to return StateUnhandled message.

                    type Service

                    type Service struct {
                    	// Testing context
                    	TB testing.TB
                    	// When AcksToDrop > 0 and it's supposed to send an ack,
                    	// the ack won't be send and AcksToDrop will decrease by 1.
                    	AcksToDrop int
                    	// Any custom HandlerFunc to be used besides DefaultHandlerFunc.
                    	Handlers map[lifxlan.MessageType]HandlerFunc
                    	// If HandlerAcks is false, AcksToDrop is ignored and you have to handle acks
                    	// in your custom HandlerFunc.
                    	// Please note that DefaultHandlerFunc doesn't handle acks.
                    	// StartService sets HandleAcks to true.
                    	HandleAcks bool
                    	// Payloads to response with DefaultHandlerFunc.
                    	RawStatePowerPayload        *lifxlan.RawStatePowerPayload
                    	RawStateLabelPayload        *lifxlan.RawStateLabelPayload
                    	RawStateVersionPayload      *lifxlan.RawStateVersionPayload
                    	RawStatePayload             *light.RawStatePayload
                    	RawStateDeviceChainPayload  *tile.RawStateDeviceChainPayload
                    	RawStateTileState64Payloads []*tile.RawStateTileState64Payload
                    	// The service context.
                    	// Please note that it's different from the context of the API calls.
                    	Context context.Context
                    	Cancel  context.CancelFunc
                    	// contains filtered or unexported fields

                      Service is a mocked device listening on localhost.

                      All service functions require TB to be non-nil, or they will panic.

                      func StartService

                      func StartService(tb testing.TB) (*Service, lifxlan.Device)

                        StartService starts a mock service, returns the service and the device.

                        func (*Service) Reply

                        func (s *Service) Reply(
                        	conn net.PacketConn,
                        	addr net.Addr,
                        	orig *lifxlan.Response,
                        	message lifxlan.MessageType,
                        	payload []byte,

                          Reply replies a request.

                          func (*Service) Start

                          func (s *Service) Start() lifxlan.Device

                            Start starts the service and returns the device.

                            func (*Service) Stop

                            func (s *Service) Stop()

                              Stop stops the mocked device service.

                              It won't response to any requests after stopped.

                              Source Files