Skip to content

Commit

Permalink
🛠️ Adding Some more 🧪 Test-cases 🧪
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsaniara committed Jul 31, 2021
1 parent 35f5070 commit 903c61f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var job = gointerlock.GoInterval{
Interval: 2 * time.Second,
Arg: myJob,
RedisHost: "localhost:6379",
RedisPassword: "myRedisPassword",
RedisPassword: "myRedisPassword", //if no pass leave it as ""
}
err := job.Run(context.Background())
if err != nil {
Expand Down
29 changes: 0 additions & 29 deletions cmd/main.go

This file was deleted.

34 changes: 34 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"fmt"
"github.com/ehsaniara/gointerlock"
"log"
"time"
)

func myJob() {
fmt.Println(time.Now(), " - called")
}

func main() {
cnx := context.Background()

//test cron
go func() {
var job = gointerlock.GoInterval{
Name: "MyTestJob",
Interval: 2 * time.Second,
Arg: myJob,
RedisHost: "localhost:6379",
RedisPassword: "MyRedisPassword",
}
err := job.Run(cnx)
if err != nil {
log.Fatalf("Error: %s", err)
}
}()

time.Sleep(10 * time.Second)
}
2 changes: 1 addition & 1 deletion goInterval.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *GoInterval) Run(ctx context.Context) error {
<-t.timer.C

//lock
locked, errLock := locker.Lock(ctx, t.Name)
locked, errLock := locker.Lock(ctx, t.Name, t.Interval)

if errLock != nil {
return errLock
Expand Down
5 changes: 3 additions & 2 deletions goIntervalLock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ type Locker struct {
redisConnector *redis.Client
}

func (s *Locker) Lock(ctx context.Context, key string) (success bool, err error) {
func (s *Locker) Lock(ctx context.Context, key string, lockTtl time.Duration) (success bool, err error) {

if s.redisConnector != nil {

if key == "" {
return false, errors.New("`Distributed Jobs should have a unique name!`")
}

res, err := s.redisConnector.SetNX(ctx, key, time.Now().String(), time.Second*15).Result()
res, err := s.redisConnector.SetNX(ctx, key, time.Now().String(), lockTtl).Result()
if err != nil {
return false, err
}
return res, nil
}
//true when lock is disabled
return true, nil
}

Expand Down
77 changes: 61 additions & 16 deletions goInterval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package gointerlock_test

import (
"context"
"flag"
"fmt"
"github.com/ehsaniara/gointerlock"
"github.com/go-redis/redis/v8"
"log"
"testing"
"time"
)

const RedisHost string = "localhost:6379"
const RedisPass string = "password"

var (
ctx = context.Background()
rdb *redis.Client
)
var (
RedisHost string
RedisPass string
)

func init() {
flag.StringVar(&RedisPass, "RedisPass", "", "Redis Password")
flag.StringVar(&RedisHost, "RedisHost", "localhost:6379", "Redis Host")
}

func init() {
rdb = redis.NewClient(&redis.Options{
Expand All @@ -29,6 +37,7 @@ func init() {
}

func ExampleNewClient() {

rdb := redis.NewClient(&redis.Options{
Addr: RedisHost, // use default Addr
Password: RedisPass, // no password set
Expand All @@ -40,19 +49,55 @@ func ExampleNewClient() {
// Output: PONG <nil>
}

func ExampleGoInterval_Run() {
func TestGoInterval_Run(t *testing.T) {
var counter = 0
var job1 = gointerlock.GoInterval{
Interval: 100 * time.Millisecond,
Arg: func() {
counter++
},
}
go func() {
_ = job1.Run(context.Background())
}()
time.Sleep(2 * time.Second)

log.Printf("counter %d", counter)
if counter != 19 {
t.Error("counter should be 19")
}

}

func TestGoInterval_LockCheck(t *testing.T) {
var counter = 0
cnx := context.Background()
var job2 = gointerlock.GoInterval{
Name: "job2",
RedisHost: RedisHost,
RedisPassword: RedisPass,
Interval: 1 * time.Second,
Arg: func() {
counter++
},
}
time.Sleep(1 * time.Second)
//instance 1 replication
go func() {
var job = gointerlock.GoInterval{
Interval: 1 * time.Second,
Arg: func() {
fmt.Print("called-")
},
}
err := job.Run(context.Background())
if err != nil {
log.Fatalf("Error: %s", err)
}
_ = job2.Run(cnx)
}()
time.Sleep(4 * time.Second)
// Output: called-called-called-
//instance 2 replication
go func() {
_ = job2.Run(cnx)
}()
//instance 3 replication
go func() {
_ = job2.Run(cnx)
}()
time.Sleep(2 * time.Second)

log.Printf("counter %d", counter)
if counter != 1 {
t.Error("counter should be 1")
}
}

0 comments on commit 903c61f

Please sign in to comment.