forked from runabol/tork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
347 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/runabol/tork" | ||
"github.com/runabol/tork/datastore" | ||
"github.com/runabol/tork/input" | ||
|
||
"github.com/runabol/tork/mq" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSubmitJob(t *testing.T) { | ||
api, err := NewAPI(Config{ | ||
DataStore: datastore.NewInMemoryDatastore(), | ||
Broker: mq.NewInMemoryBroker(), | ||
}) | ||
assert.NoError(t, err) | ||
|
||
req, err := http.NewRequest("GET", "/", nil) | ||
assert.NoError(t, err) | ||
w := httptest.NewRecorder() | ||
|
||
ctx := Context{api: api, ctx: echo.New().NewContext(req, w)} | ||
|
||
called := false | ||
listener := func(j *tork.Job) { | ||
called = true | ||
assert.Equal(t, tork.JobStateCompleted, j.State) | ||
} | ||
|
||
j, err := ctx.SubmitJob(&input.Job{ | ||
Name: "test job", | ||
Tasks: []input.Task{ | ||
{ | ||
Name: "first task", | ||
Image: "some:image", | ||
}, | ||
}, | ||
}, listener) | ||
assert.NoError(t, err) | ||
|
||
j.State = tork.JobStateCompleted | ||
|
||
err = api.broker.PublishEvent(context.Background(), mq.TOPIC_JOB_COMPLETED, j) | ||
assert.NoError(t, err) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
assert.True(t, called) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package wildcard | ||
|
||
// credit: https://github.com/vodkaslime/wildcard | ||
|
||
const C = '*' | ||
|
||
func isWildPattern(pattern string) bool { | ||
for i := range pattern { | ||
c := pattern[i] | ||
if c == C { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func Match(pattern string, s string) bool { | ||
// Edge cases. | ||
if pattern == string(C) { | ||
return true | ||
} | ||
|
||
if pattern == "" { | ||
return s == "" | ||
} | ||
|
||
// If pattern does not contain wildcard chars, just compare the strings | ||
// to avoid extra memory allocation. | ||
if !isWildPattern(pattern) { | ||
return pattern == s | ||
} | ||
|
||
// Initialize DP. | ||
lp := len(pattern) | ||
ls := len(s) | ||
dp := make([][]bool, lp+1) | ||
for i := 0; i < lp+1; i++ { | ||
dp[i] = make([]bool, ls+1) | ||
} | ||
|
||
dp[0][0] = true | ||
|
||
for i := 0; i < lp; i++ { | ||
if pattern[i] == C { | ||
dp[i+1][0] = dp[i][0] | ||
} else { | ||
dp[i+1][0] = false | ||
} | ||
} | ||
|
||
for j := 0; j < ls; j++ { | ||
dp[0][j+1] = false | ||
} | ||
|
||
// Start DP. | ||
for i := 0; i < lp; i++ { | ||
for j := 0; j < ls; j++ { | ||
pc := pattern[i] | ||
sc := s[j] | ||
switch pattern[i] { | ||
case C: | ||
dp[i+1][j+1] = dp[i][j] || dp[i][j+1] || dp[i+1][j] | ||
default: | ||
if pc == sc { | ||
dp[i+1][j+1] = dp[i][j] | ||
} else { | ||
dp[i+1][j+1] = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
return dp[lp][ls] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package wildcard | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type wildPatternTestCase struct { | ||
p string | ||
m bool | ||
} | ||
|
||
type matchTestCase struct { | ||
p string | ||
s string | ||
m bool | ||
} | ||
|
||
func TestIsWildPattern(t *testing.T) { | ||
testCases1 := []wildPatternTestCase{ | ||
{"*", true}, | ||
{"**", true}, | ||
{".", false}, | ||
{"a", false}, | ||
} | ||
|
||
for _, tc := range testCases1 { | ||
b := isWildPattern(tc.p) | ||
if !assert.Equal(t, b, tc.m) { | ||
println(tc.p, tc.m) | ||
} | ||
} | ||
|
||
} | ||
|
||
func TestMatch(t *testing.T) { | ||
|
||
testCases1 := []matchTestCase{ | ||
{"", "", true}, | ||
{"*", "", true}, | ||
{"", "a", false}, | ||
{"abc", "abc", true}, | ||
{"abc", "ac", false}, | ||
{"abc", "abd", false}, | ||
{"a*c", "abc", true}, | ||
{"a*c", "abcbc", true}, | ||
{"a*c", "abcbd", false}, | ||
{"a*b*c", "ajkembbcldkcedc", true}, | ||
} | ||
|
||
for _, tc := range testCases1 { | ||
m := Match(tc.p, tc.s) | ||
if !assert.Equal(t, m, tc.m) { | ||
println(tc.p, tc.s, tc.m) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
func TestMatch2(t *testing.T) { | ||
testCases1 := []matchTestCase{ | ||
{"jobs.*", "jobs.completed", true}, | ||
{"jobs.*", "jobs.long.completed", true}, | ||
{"tasks.*", "jobs.completed", false}, | ||
{"*.completed", "jobs.completed", true}, | ||
{"*.completed.thing", "jobs.completed", false}, | ||
} | ||
for _, tc := range testCases1 { | ||
m := Match(tc.p, tc.s) | ||
assert.Equal(t, m, tc.m) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.