Coder Social home page Coder Social logo

Comments (6)

stevehill1981 avatar stevehill1981 commented on August 23, 2024

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.

lanybass avatar lanybass commented on August 23, 2024

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.

lanybass avatar lanybass commented on August 23, 2024

I think this project is dead .. no udpating, no issue closing

from gearman-go.

mikespook avatar mikespook commented on August 23, 2024

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.

stevehill1981 avatar stevehill1981 commented on August 23, 2024

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.

No-ops avatar No-ops commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.