Skip to content

Commit

Permalink
don't block if no progress is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackie Li committed Apr 26, 2019
1 parent 61ada2f commit b8072fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 10 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ func (p JobQuery) ProgressChanNoWait(jobID string) chan<- string {
for {
select {
case progress := <-lpch:
if progress == "" {
continue
}
// println("setting progress", jobID, progress)
err := p.SetProgress(jobID, progress)
if err != nil {
log.WARNING.Printf("failed to set progress for job id %s: %s, err: %v", jobID, progress, err)
Expand Down Expand Up @@ -410,7 +414,12 @@ func (p JobQuery) ProgressChanNoWait(jobID string) chan<- string {
close(done)
return
default:
progress = <-pch
select {
case progress = <-pch:
case <-p.done:
close(done)
return
}
}
}
}()
Expand Down
18 changes: 12 additions & 6 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func ExampleProcess() {
defer p.Close()

interruptedChan := make(chan struct{})
println(interruptedChan)
done := make(chan struct{})

go func() {
Expand Down Expand Up @@ -254,7 +253,6 @@ func TestChannelAPI(t *testing.T) {
defer j.Close()

interruptedChan := j.InterruptedChan(jobID)
println(interruptedChan)
processChan := j.ProgressChan(jobID)

// emulate a long running task
Expand Down Expand Up @@ -405,15 +403,12 @@ func TestWithInterruptCtx(t *testing.T) {
}

func TestProgressNoWait(t *testing.T) {

c := redigomock.NewConn()
jobID := uuid.New().String()
jq := NewJobQuery(c)

ch := jq.ProgressChanNoWait(jobID)
cmd := c.Command("SET")
c.Command("EXPIRE")
c.Command("FLUSH")

for i := 0; i < 10; i++ {
// time.Sleep(100 * time.Millisecond)
Expand All @@ -422,5 +417,16 @@ func TestProgressNoWait(t *testing.T) {
require.NoError(t, jq.Close())
require.NoError(t, c.Err())

assert.Equal(t, c.Stats(cmd), 1, "should be called once")
assert.Truef(t, 10 > c.Stats(cmd), "should be called once")
}

func TestProgressNoWaitNonBlock(t *testing.T) {

c := redigomock.NewConn()
jobID := uuid.New().String()
jq := NewJobQuery(c)

jq.ProgressChanNoWait(jobID)
time.Sleep(100 * time.Millisecond)
jq.Close()
}

0 comments on commit b8072fc

Please sign in to comment.