-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a MockCluster within Go for testing #729
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great stuff!
Hi @edenhill, still something missing? |
Indeed, it would be great to get this merged! I've got a project itching to use it :) |
Let's wait for a CI test before the merge. |
kafka/mockcluster.go
Outdated
|
||
mc.cConf = C.rd_kafka_conf_new() | ||
|
||
mc.rk = C.rd_kafka_new(C.RD_KAFKA_PRODUCER, mc.cConf, cErrstr, 256) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an issue here when Close()
is called which causes a crash.
In the documentation for rd_kafka_new it says:
conf is an optional struct created with rd_kafka_conf_new() that will be used instead of the default configuration. The conf object is freed by this function on success and must not be used or destroyed by the application sub-sequently. See rd_kafka_conf_set() et.al for more information.
This means that on success there should be no calls to C.rd_kafka_conf_destroy(mc.cConf)
which is done in Close()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I removed the call to the conf_destroy function to prevent problems ;-) Thanks
@@ -0,0 +1,100 @@ | |||
package main | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to mockcluster_example instead, to match the API naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
c, err := kafka.NewConsumer(&kafka.ConfigMap{ | ||
"bootstrap.servers": broker, | ||
// Avoid connecting to IPv6 brokers: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
kafka/mockcluster.go
Outdated
* limitations under the License. | ||
*/ | ||
|
||
package kafka |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the package as the first line of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
kafka/mockcluster.go
Outdated
|
||
package kafka | ||
|
||
import "C" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate import, remove this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok done
kafka/mockcluster.go
Outdated
|
||
package kafka | ||
|
||
import "C" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate import, remove this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
kafka/mockcluster.go
Outdated
// @remark This is an experimental public API that is NOT covered by the | ||
// librdkafka API or ABI stability guarantees. | ||
// | ||
// @warning THIS IS AN EXPERIMENTAL API, SUBJECT TO CHANGE OR REMOVAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// @warning THIS IS AN EXPERIMENTAL API, SUBJECT TO CHANGE OR REMOVAL. | |
// Warning: THIS IS AN EXPERIMENTAL API, SUBJECT TO CHANGE OR REMOVAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
kafka/mockcluster.go
Outdated
cErrstr := (*C.char)(C.malloc(C.size_t(256))) | ||
defer C.free(unsafe.Pointer(cErrstr)) | ||
|
||
mc.cConf = C.rd_kafka_conf_new() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to store this on the MockCluster object, it is only used in this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the config reference from MockCluster
kafka/mockcluster.go
Outdated
|
||
mc := &MockCluster{} | ||
|
||
cErrstr := (*C.char)(C.malloc(C.size_t(256))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this 512 to avoid truncation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok done
kafka/mockcluster.go
Outdated
mc.mcluster = C.rd_kafka_mock_cluster_new(mc.rk, C.int(brokerCount)) | ||
if mc.mcluster == nil { | ||
C.rd_kafka_destroy(mc.rk) | ||
C.rd_kafka_conf_destroy(mc.cConf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line, the cConf is already freed by the successful rd_kafka_new call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. removed the call
kafka/mockcluster.go
Outdated
} | ||
|
||
// Bootstrapservers returns the bootstrap.servers property for this MockCluster | ||
func (mc *MockCluster) Bootstrapservers() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to BootstrapServers()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Ok. Sorry for the single commits. @edenhill changed the impl according to your suggestion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I can't merge this just yet because we need you to sign the CLA, and we're in the midst of the process of switching CLA signing solutions. |
Co-authored-by: Magnus Edenhill <[email protected]>
Co-authored-by: Magnus Edenhill <[email protected]>
Co-authored-by: Magnus Edenhill <[email protected]>
Co-authored-by: Magnus Edenhill <[email protected]>
Co-authored-by: Magnus Edenhill <[email protected]>
Co-authored-by: Magnus Edenhill <[email protected]>
@edenhill it looks like this is ready to go including CLA... any chance of a merge with it? We're itching to use it... Also, many thanks for all the hard work @SourceFellows! Update: I was thinking even if it's on |
Thank you for this! |
Thanks for the quick merge! |
I think a blog post how to use this as part of application testing would be fantastic.. just saying..saying |
hearing that ;-) i started but not finished yet... going soon |
…confluentinc#729) * Added MockCluster implementation which can be used for testing * adjust documentation for mock cluster * Added example for MockCluster * Fixed naming Co-authored-by: Magnus Edenhill <[email protected]> * fixed typo Co-authored-by: Magnus Edenhill <[email protected]> * removed Co-authored-by: Magnus Edenhill <[email protected]> * fixed comment Co-authored-by: Magnus Edenhill <[email protected]> * adjust documentation Co-authored-by: Magnus Edenhill <[email protected]> * adjustments regarding comments from Magnus * fixed cleanup code for kafka configuration * fixed destroy for C references * fixed spaces * removed call to `C.rd_kafka_conf_destroy(mc.cConf)` in close * added docu for MockCluster type * moved package statement into first line * adjustments according to @edenhill comments * Update kafka/mockcluster.go Co-authored-by: Magnus Edenhill <[email protected]> * removed duplicate import * removed the remark as suggested from @edelhill * changed typedef from struct_rd_kafka_mock_cluster_s to rd_kafka_mock_cluster_t Co-authored-by: Kristian Köhler <[email protected]> Co-authored-by: Magnus Edenhill <[email protected]> Co-authored-by: Kristian Köhler <[email protected]>
Hi,
this PR exposes the MockCluster from librdkafka to Golang.
You are able to start a MockCluster with:
kafka.NewMockCluster(1)
and read the corresponding bootstrap.servers property via:
broker := mockCluster.Bootstrapservers()
Remote communication is also possible.
An example is also added.
Thanks,
Kristian