From 77214fa9fec5543fc2f9e34268932d6e46dc6e62 Mon Sep 17 00:00:00 2001 From: g41797 Date: Mon, 5 Feb 2024 07:59:52 +0200 Subject: [PATCH] Mon Feb 5 07:59:38 AM IST 2024 --- longjob_test.go | 67 ++++++++++++++++++++++++++++++++--- root_test.go | 5 --- testdata/longjobpayload1.json | 4 +++ testdata/wrong.json | 1 + 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 testdata/longjobpayload1.json create mode 100644 testdata/wrong.json diff --git a/longjob_test.go b/longjob_test.go index 04370a6..cac87e0 100644 --- a/longjob_test.go +++ b/longjob_test.go @@ -2,15 +2,19 @@ package jobnik_test import ( "context" + _ "embed" "encoding/json" "fmt" + "testing" "github.com/g41797/jobnik" ) +const longJobJobnikName = "ProcessLongJob" + type longJobHandler struct { - loops int `json:"LoopsCount"` - slpmls int `json:"SleepMlsec"` + Loops int `json:"LoopsCount"` + Slpmls int `json:"SleepMlsec"` } // Factory function @@ -20,7 +24,7 @@ func longJobHandlerFactory() (jobnik.Jobnik, error) { // Register handler: func init() { - jobnik.RegisterFactory("ProcessLongJob", longJobHandlerFactory) + jobnik.RegisterFactory(longJobJobnikName, longJobHandlerFactory) } // Works without initial configuration @@ -30,8 +34,8 @@ func (jh *longJobHandler) InitOnce(_ string) error { } func (jh *longJobHandler) setDefaults() { - jh.loops = 100 - jh.slpmls = 1 + jh.Loops = 100 + jh.Slpmls = 1 return } @@ -57,3 +61,56 @@ func (jh *longJobHandler) Process(cncl context.Context, job jobnik.Job) (jobnik. } return jobnik.JobStatus{}, fmt.Errorf("ProcessLongJob is not implemented yet") } + +//---------------------------------------------- +// Useful links for using embedded folders/files +//---------------------------------------------- +// https://pkg.go.dev/embed +// https://gobyexample.com/embed-directive +// https://www.iamyadav.com/blogs/a-guide-to-embedding-static-files-in-go + +//go:embed testdata/wrong.json +var wj string + +func TestWrongJSON(t *testing.T) { + if _, err := jobnik.NewJobOrder("name", nil, wj, false); err == nil { + t.Errorf("error == nil for wrong json") + } +} + +//go:embed testdata/longjobpayload1.json +var ljp1 string + +func TestRightJSON(t *testing.T) { + if _, err := jobnik.NewJobOrder("name", nil, ljp1, false); err != nil { + t.Errorf("error == nil for right json") + } +} + +func TestProcess(t *testing.T) { + if _, err := jobnik.NewJobnik("UNKNOWN"); err == nil { + t.Errorf("error == nil for non existing jobnik") + } + + job, _ := jobnik.NewJob("id", longJobJobnikName, nil, ljp1) + + jbnk, err := jobnik.NewJobnik(job.Name()) + if err != nil { + t.Errorf("error != nil for existing jobnik %s", job.Name()) + } + + if err = jbnk.InitOnce(""); err != nil { + t.Errorf("error %s for InitOnce", err.Error()) + } + defer jbnk.FinishOnce() + + jbst, err := jbnk.Process(context.Background(), job) + + if err != nil { + t.Errorf("error %s for Process", err.Error()) + } + + if jbst.State != jobnik.Finished { + t.Errorf("wrong job state %s ", jbst.State) + } +} diff --git a/root_test.go b/root_test.go index 06b5a36..617c8f8 100644 --- a/root_test.go +++ b/root_test.go @@ -2,8 +2,3 @@ // SPDX-License-Identifier: MIT package jobnik_test - -import "testing" - -func TestMain(m *testing.M) { -} diff --git a/testdata/longjobpayload1.json b/testdata/longjobpayload1.json new file mode 100644 index 0000000..ccb885d --- /dev/null +++ b/testdata/longjobpayload1.json @@ -0,0 +1,4 @@ +{ + "LoopsCount": 1000, + "SleepMlsec": 1 +} \ No newline at end of file diff --git a/testdata/wrong.json b/testdata/wrong.json new file mode 100644 index 0000000..5ef6710 --- /dev/null +++ b/testdata/wrong.json @@ -0,0 +1 @@ +==================== \ No newline at end of file