Permalink
Browse files

tested increment progress, abort and set maximum

  • Loading branch information...
1 parent 3d0d391 commit 961e7a7a0b737aec374442908e81927acabb3bbd @fterrier committed Oct 10, 2012
@@ -1,6 +1,6 @@
class RabbitmqTasksGrailsPlugin {
// the plugin version
- def version = "0.5"
+ def version = "0.5.1"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3.3 > *"
// the other plugins this plugin depends on
@@ -23,7 +23,7 @@ abstract class Task implements Progress {
// progress
Long max = 0;
Long current = null;
- Boolean aborted = false;
+ Boolean aborted;
abstract def executeTask()
abstract boolean isUnique()
@@ -53,10 +53,10 @@ abstract class Task implements Progress {
void incrementProgress(Long increment = null) {
if (log.isDebugEnabled()) log.debug('incrementProgress, max: '+ max +', current: '+current)
- if (aborted) throw new TaskAbortedException()
if (current != null) {
Task.withNewTransaction {
+ if (aborted) throw new TaskAbortedException()
if (increment == null) current++
else current += increment
this.save(flush: true)
@@ -36,20 +36,22 @@ class TaskService {
}
if (task != null && task.status != TaskStatus.COMPLETED && task.status != TaskStatus.ABORTED) {
- // we set the status to in_progress
- Task.withTransaction {
- task.status = TaskStatus.IN_PROGRESS
- task.numberOfTries++
- task.started = new Date()
- task.save(failOnError: true)
- }
-
- // we execute the task
- try {
- if (!task.aborted) task.executeTask()
- }
- catch (TaskAbortedException e) {
- if (log.isInfoEnabled()) log.info('task aborted: '+task)
+ if (!task.aborted) {
+ // we set the status to in_progress
+ Task.withTransaction {
+ task.status = TaskStatus.IN_PROGRESS
+ task.numberOfTries++
+ task.started = new Date()
+ task.save(failOnError: true)
+ }
+
+ // we execute the task
+ try {
+ task.executeTask()
+ }
+ catch (TaskAbortedException e) {
+ if (log.isInfoEnabled()) log.info('task aborted: '+task)
+ }
}
Task.withTransaction {
@@ -146,7 +146,7 @@ class TaskControllerSpec extends IntegrationSpec {
// }
- def "delete new task already sent aborts the task"() {
+ def "delete new task already sent deletes the task"() {
setup:
def task = new DummyTask(dataId: 1, principal: 'uuid', status: TaskStatus.NEW, sentToQueue: true).save(failOnError: true)
taskController = new TaskController()
@@ -28,4 +28,78 @@ class TaskDomainSpec extends IntegrationSpec {
thrown ValidationException
}
+ def "increment progress on aborted class throws exception"() {
+ setup:
+ def task = new DummyTask(principal: 'principal', status: TaskStatus.IN_PROGRESS, dataId: 1).save(failOnError: true)
+ task.max = 1
+ task.current = 0
+ task.aborted = true
+
+ when:
+ task.incrementProgress()
+
+ then:
+ thrown TaskAbortedException
+ }
+
+ def "abort"() {
+ setup:
+ def task
+ Task.withNewTransaction {
+ task = new DummyTask(principal: 'principal', status: TaskStatus.IN_PROGRESS, dataId: 1).save(failOnError: true)
+ }
+
+ when:
+ task.abort()
+
+ then:
+ task.aborted == true
+
+ cleanup:
+ Task.withNewTransaction {
+ DummyTask.list().each{it.delete(flush: true)}
+ }
+ }
+
+ def "set maximum"() {
+ setup:
+ def task
+ Task.withNewTransaction {
+ task = new DummyTask(principal: 'principal', status: TaskStatus.IN_PROGRESS, dataId: 1).save(failOnError: true)
+ }
+
+ when:
+ task.setMaximum(100)
+
+ then:
+ task.current == 0
+ task.max == 100
+
+ cleanup:
+ Task.withNewTransaction {
+ DummyTask.list().each{it.delete(flush: true)}
+ }
+ }
+
+ def "increment progress"() {
+ setup:
+ def task
+ Task.withNewTransaction {
+ task = new DummyTask(principal: 'principal', status: TaskStatus.IN_PROGRESS, dataId: 1).save(failOnError: true)
+ task.max = 1
+ task.current = 0
+ }
+
+ when:
+ task.incrementProgress()
+
+ then:
+ task.current == 1
+
+ cleanup:
+ Task.withNewTransaction {
+ DummyTask.list().each{it.delete(flush: true)}
+ }
+ }
+
}
@@ -73,6 +73,33 @@ class TaskServiceSpec extends IntegrationSpec {
notThrown RuntimeException
}
+ def "execute task does not do anything if task is aborted by user and sets aborted status"() {
+ setup:
+ def task = new DummyTask(principal: 'principal', status: TaskStatus.IN_PROGRESS, dataId: 1).save(failOnError:true)
+ task.aborted = true
+ task.metaClass.executeTask {throw new RuntimeException()}
+
+ when:
+ taskService.executeTask(task.id)
+
+ then:
+ notThrown RuntimeException
+ Task.list()[0].status == TaskStatus.ABORTED
+ }
+
+ def "execute task throwing TaskAbortException is set as aborted"() {
+ setup:
+ def task = new DummyTask(principal: 'principal', status: TaskStatus.NEW, dataId: 1).save(failOnError:true)
+ task.metaClass.executeTask {task.aborted = true; throw new TaskAbortedException();}
+
+ when:
+ taskService.executeTask(task.id)
+
+ then:
+ notThrown TaskAbortedException
+ Task.list()[0].status == TaskStatus.ABORTED
+ }
+
def "task is set as aborted when abort exception is thrown"() {
setup:
def task = new DummyTask(principal: 'principal', status: TaskStatus.NEW, dataId: 1).save(failOnError:true)

0 comments on commit 961e7a7

Please sign in to comment.