Comments (6)
I think I've figured this out now, though not necessarily in the best way. Here's my test script:
package main
import (
"testing"
"github.com/mikespook/gearman-go/worker"
"crypto/sha256"
)
type TestJob struct {
JobHandle string
JobId string
JobData string
}
func (tj TestJob) Err() error {
return nil
}
func (tj TestJob) Data() []byte {
return []byte(tj.JobData)
}
func (tj TestJob) Fn() string {
return "TestJob"
}
func (tj TestJob) SendWarning(data []byte) {
// NOOP
}
func (tj TestJob) SendData(data []byte) {
// NOOP
}
func (tj TestJob) UpdateStatus(numerator, denominator int) {
// NOOP
}
func (tj TestJob) Handle() string {
return tj.JobHandle
}
func (tj TestJob) UniqueId() string {
return tj.JobId
}
func createJob(data []byte) (job worker.Job, err error) {
hasher := sha256.New()
hasher.Write(data)
jobHandle := string(hasher.Sum(nil))
jobData := string(data)
jobId := jobHandle[0:16]
newJob := TestJob{JobId: jobId, JobHandle: jobHandle, JobData: jobData}
return newJob, nil
}
func TestDisplayMessage(t *testing.T) {
expected := "HELLO"
job, err := createJob([]byte("Hello"))
if err != nil {
t.Error("Couldn't create the job.")
}
actual, err := DisplayMessage(job)
if string(actual) != expected {
t.Errorf("Expected '%s', got '%s'", expected, string(actual))
}
}
Is there an easier way to get to this? :)
from gearman-go.
It's not about "new to go", but "new to programing" ?
you don't need to call the func DisplayMessage
,so you don't need make a Job
, it's a callback function, just register (see https://github.com/mikespook/gearman-go/blob/master/example/worker/worker.go)
then send a command on another PC by Client.go to the gearman-server. if worker is running and is connected to the server,it could received the msg and call the func DisplayMessage
from gearman-go.
I think this project is dead .. no udpating, no issue closing
from gearman-go.
Hi @stevehill1981
You can't test the worker directly unless you write your own dummy-job and pass it to the callback.
But if you want to combine the testing of client and worker, you need Gearman server involved in.
It's not a good practical thing, isn't it? So, I abandoned Gearman from the architecture of my projects.
Hi @lanybass
Dying, but not dead.
The Gearman protocol has some bad design inside and personally, I've used MQTT to replace Gearman recent years.
The issue will keep open unless I think we have all the facts we need. Still, I will try to fix the significant issue but won't improve the library.
And If someone can relive this project, I'd like to transfer the project (or just make a clone and let me know).
from gearman-go.
Thanks @mikespook. I'm stuck with Gearman for the time being for legacy reasons, but perhaps, if I'm rewriting the workers anyway (moving from PHP to Go), I can justify changing the queueing system at the same time.
@lanybass - I wanted to test just the functionality of DisplayMessage
; not the Gearman worker part of it. I'm coming from the PHP world (which may or may not make me a proper programmer, despite more than 10 years of experience), and when I was doing a similar thing there I had to extend the GearmanJob
class so that I could set the appropriate data without having to run a full client/server setup. Apologies if this doesn't give me street cred as a real programmer.
from gearman-go.
I would highly recommend abstracting the functionality you use gearman for in your project to an interface. This way you can mock the gearman functionality in a unit test. Anything involving the actual gearman worker would be an integration test.
For this specific case you can just create a mock struct. You can see the structure you need for it here: https://github.com/mikespook/gearman-go/blob/master/worker/job.go
from gearman-go.
Related Issues (20)
- invalid magic HOT 6
- 循环调用同个work,一分钟左右就卡住了
- Custom work logic HOT 8
- 在大量的提交时,会出现map并发写错误 HOT 1
- example中的jobhandler
- example client.go中的jobhandler没有被调用 HOT 1
- Calling Multiple Ready() results in workers registered multiple times
- Race condition when creating new job from client HOT 1
- Client need to receive a JOB_CREATED from server before submitting a new job HOT 1
- Can job be killed by client? HOT 2
- When there is only one worker, and this worker is blocked and client continues to submit ,runnning jobs more than worker HOT 1
- Waiting until all jobs had been completed HOT 1
- worker连续两次grab会导致获取不到任务 HOT 2
- Unsolicited response received on idle HTTP channel
- Readme out of date.
- Race condition on close worker HOT 3
- Worker doesn't continue work after limit
- client.SelectWithRate Always choose the first one,Right?
- client/pool.go有死锁问题, client.Lock() 应该改为 client.mutex.Lock() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gearman-go.