Skip to content

Commit

Permalink
test: opimize test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Aug 26, 2022
1 parent 1d377a4 commit 18b8a94
Show file tree
Hide file tree
Showing 21 changed files with 33 additions and 207 deletions.
17 changes: 2 additions & 15 deletions glogger/local_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
*
*/
type LogWriter struct {
file *os.File
logSlot []string
maxSlotCount int
file *os.File
}

func NewLogWriter(filepath string, maxSlotCount int) *LogWriter {
Expand All @@ -22,23 +20,12 @@ func NewLogWriter(filepath string, maxSlotCount int) *LogWriter {
os.Exit(1)
}

return &LogWriter{file: logFile,
logSlot: make([]string, maxSlotCount),
}
return &LogWriter{file: logFile}
}
func (lw *LogWriter) Write(b []byte) (n int, err error) {
if len(lw.logSlot) > lw.maxSlotCount {
lw.logSlot = append(lw.logSlot[1:], string(b))
} else {
lw.logSlot = append(lw.logSlot, string(b))
}

return lw.file.Write(b)
}

func (lw *LogWriter) Slot() []string {
return lw.logSlot
}
func (lw *LogWriter) Close() error {
if lw.file != nil {
return lw.file.Close()
Expand Down
6 changes: 0 additions & 6 deletions plugin/http_server/user_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/typex"

"github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -102,11 +101,6 @@ func Logs(c *gin.Context, hh *HttpApiServer, e typex.RuleX) {
Content string `json:"content" binding:"required"`
}
logs := []Data{}
for i, s := range glogger.GLOBAL_LOGGER.Slot() {
if s != "" {
logs = append(logs, Data{i, s})
}
}
c.JSON(200, Result{
Code: 200,
Msg: SUCCESS,
Expand Down
11 changes: 1 addition & 10 deletions test/device_485ther_gw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package test
import (
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

"testing"
Expand All @@ -19,13 +16,7 @@ import (
*
*/
func Test_modbus_485_sensor_gateway(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

hh := httpserver.NewHttpApiServer()
Expand Down
11 changes: 1 addition & 10 deletions test/device_generic_modbus_device_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package test

import (
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

Expand All @@ -13,14 +11,7 @@ import (
)

func Test_Generic_modbus_device(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
core.GlobalConfig.AppDebugMode = true
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

hh := httpserver.NewHttpApiServer()
Expand Down
11 changes: 1 addition & 10 deletions test/device_generic_snmp_device_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package test

import (
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

Expand All @@ -20,14 +18,7 @@ import (
// "PCTotalMemory":33262700
// }
func Test_Generic_snmp_device(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
core.GlobalConfig.AppDebugMode = true
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

hh := httpserver.NewHttpApiServer()
Expand Down
12 changes: 1 addition & 11 deletions test/device_generic_uart_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package test
import (
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

"testing"
Expand All @@ -19,14 +16,7 @@ import (
*
*/
func Test_UART_Device(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
core.GlobalConfig.AppDebugMode = true
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

// HttpApiServer loaded default
Expand Down
10 changes: 1 addition & 9 deletions test/device_gos7_plc_data_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"testing"
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/rulexrpc"
Expand All @@ -17,13 +15,7 @@ import (
)

func Test_S7_PLC_Parse(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()


Expand Down
17 changes: 3 additions & 14 deletions test/device_s1200plc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package test
import (
"encoding/json"
"os"
"os/signal"
"syscall"

"testing"

"github.com/i4de/rulex/common"
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"

"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/typex"
)
Expand Down Expand Up @@ -98,15 +96,7 @@ func Test_parse_config(t *testing.T) {
*
*/
func Test_RULEX_WITH_S1200PLC(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM)
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

S1200PLC := typex.NewDevice(typex.S1200PLC,
Expand Down Expand Up @@ -176,7 +166,6 @@ func Test_RULEX_WITH_S1200PLC(t *testing.T) {
t.Error(err)
}

s := <-c
glogger.GLogger.Warn("Received stop signal:", s)
engine.Stop()
os.Exit(0)
Expand Down
11 changes: 1 addition & 10 deletions test/device_th_485_sensor_data_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"testing"
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/rulexrpc"
"github.com/i4de/rulex/typex"
Expand All @@ -23,13 +20,7 @@ import (
*
*/
func Test_modbus_485_sensor_data_parse(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()


Expand Down
12 changes: 2 additions & 10 deletions test/device_tss200_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package test

import (
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"

"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

Expand All @@ -13,14 +12,7 @@ import (
)

func Test_TSS200_ReadData(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
core.GlobalConfig.AppDebugMode = true
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

hh := httpserver.NewHttpApiServer()
Expand Down
12 changes: 1 addition & 11 deletions test/device_yk8_with_rulex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package test
import (
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"

"testing"
Expand All @@ -19,14 +16,7 @@ import (
*
*/
func Test_modbus_485_yk8(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
core.GlobalConfig.AppDebugMode = true
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

// HttpApiServer loaded default
Expand Down
11 changes: 1 addition & 10 deletions test/fully_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package test

import (
"context"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/rulexrpc"
Expand All @@ -18,13 +15,7 @@ import (
)

func TestFullyRun(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

if err := engine.LoadPlugin("plugin.http_server", httpserver.NewHttpApiServer()); err != nil {
Expand Down
11 changes: 1 addition & 10 deletions test/jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"testing"
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/rulexrpc"
Expand All @@ -17,16 +15,9 @@ import (
)

func Test_JQ_Parse(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()


hh := httpserver.NewHttpApiServer()
// HttpApiServer loaded default
if err := engine.LoadPlugin("plugin.http_server", hh); err != nil {
Expand Down
10 changes: 1 addition & 9 deletions test/rpc_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"testing"
"time"

"github.com/i4de/rulex/core"
"github.com/i4de/rulex/engine"
"github.com/i4de/rulex/glogger"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/rulexrpc"
Expand Down Expand Up @@ -54,13 +52,7 @@ func _startServer() {

}
func Test_Codec(t *testing.T) {
mainConfig := core.InitGlobalConfig("conf/rulex.ini")
glogger.StartGLogger(true, core.GlobalConfig.LogPath)
glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath)
core.StartStore(core.GlobalConfig.MaxQueueSize)
core.SetLogLevel()
core.SetPerformance()
engine := engine.NewRuleEngine(mainConfig)
engine := RunTestEngine()
engine.Start()

_startServer()
Expand Down
Loading

0 comments on commit 18b8a94

Please sign in to comment.