// checkRecovering checks if the backend can be recovered. func (m *systemMonitor) checkRecovering(rs loop.Recoverings) (loop.Recoverings, error) { if rs.Frequency(12, time.Minute) { logger.Errorf("monitor cannot be recovered: %v", rs.Last().Reason) return nil, errors.New(ErrMonitorCannotBeRecovered, errorMessages, rs.Last().Reason) } logger.Warningf("monitor recovered: %v", rs.Last().Reason) return rs.Trim(12), nil }
// command sends a command to the system monitor and waits for a response. func (m *systemMonitor) command(opCode int, args interface{}) (interface{}, error) { cmd := &command{opCode, args, make(chan interface{})} m.commandChan <- cmd resp, ok := <-cmd.respChan if !ok { return nil, errors.New(ErrMonitorPanicked, errorMessages) } if err, ok := resp.(error); ok { return nil, err } return resp, nil }
// Test the validation. func TestValidation(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) ec := 1 messages := errors.Messages{ec: "valid"} err := errors.New(ec, messages) fileName, line, lerr := errors.Location(err) assert.True(errors.Valid(err)) assert.Nil(lerr) assert.Equal(fileName, "errors_test.go") assert.Equal(line, 31) }
// Test the validation. func TestValidation(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) ec := 1 messages := errors.Messages{ec: "valid"} err := errors.New(ec, messages) packageName, fileName, line, lerr := errors.Location(err) assert.True(errors.Valid(err)) assert.Nil(lerr) assert.Equal(packageName, "github.com/tideland/goas/v3/errors_test") assert.Equal(fileName, "errors_test.go") assert.Equal(line, 31) }
// Test creation and checking. func TestIsError(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) ec := 42 messages := errors.Messages{ec: "test error %d"} err := errors.New(ec, messages, 1) assert.ErrorMatch(err, `\[ERRORS_TEST:042\] test error 1`) assert.True(errors.IsError(err, ec)) assert.False(errors.IsError(err, 0)) err = testError("test error 2") assert.ErrorMatch(err, "test error 2") assert.False(errors.IsError(err, ec)) assert.False(errors.IsError(err, 0)) }
// processCommand handles the received commands of the monitor. func (m *systemMonitor) processCommand(cmd *command) { defer cmd.close() switch cmd.opCode { case cmdReset: // Reset monitoring. m.init() cmd.ok() case cmdMeasuringPointRead: // Read just one measuring point. id := cmd.args.(string) if mp, ok := m.etmData[id]; ok { // Measuring point found. clone := *mp cmd.respond(&clone) } else { // Measuring point does not exist. cmd.respond(errors.New(ErrMeasuringPointNotExists, errorMessages, id)) } case cmdMeasuringPointsReadAll: // Read all measuring points. resp := MeasuringPoints{} for _, mp := range m.etmData { clone := *mp resp = append(resp, &clone) } sort.Sort(resp) cmd.respond(resp) case cmdStaySetVariableRead: // Read just one stay-set variable. id := cmd.args.(string) if ssv, ok := m.ssvData[id]; ok { // Variable found. clone := *ssv cmd.respond(&clone) } else { // Variable does not exist. cmd.respond(errors.New(ErrStaySetVariableNotExists, errorMessages, id)) } case cmdStaySetVariablesReadAll: // Read all stay-set variables. resp := StaySetVariables{} for _, mp := range m.ssvData { clone := *mp resp = append(resp, &clone) } sort.Sort(resp) cmd.respond(resp) case cmdDynamicStatusRetrieverRead: // Read just one dynamic status value. id := cmd.args.(string) if dsr, ok := m.dsrData[id]; ok { // Dynamic status found. v, err := dsr() if err != nil { cmd.respond(err) } else { cmd.respond(v) } } else { // Dynamic status does not exist. cmd.respond(errors.New(ErrDynamicStatusNotExists, errorMessages, id)) } case cmdDynamicStatusRetrieversReadAll: // Read all dynamic status values. resp := DynamicStatusValues{} for id, dsr := range m.dsrData { v, err := dsr() if err != nil { cmd.respond(err) } dsv := &DynamicStatusValue{id, v} resp = append(resp, dsv) } sort.Sort(resp) cmd.respond(resp) } }