Skip to content

Commit

Permalink
Mon Feb 5 07:59:38 AM IST 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Feb 5, 2024
1 parent c2fb419 commit 77214fa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
67 changes: 62 additions & 5 deletions longjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
}
5 changes: 0 additions & 5 deletions root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@
// SPDX-License-Identifier: MIT

package jobnik_test

import "testing"

func TestMain(m *testing.M) {
}
4 changes: 4 additions & 0 deletions testdata/longjobpayload1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"LoopsCount": 1000,
"SleepMlsec": 1
}
1 change: 1 addition & 0 deletions testdata/wrong.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
====================

0 comments on commit 77214fa

Please sign in to comment.