Skip to content

Commit

Permalink
Merge pull request #60 from tinchodias/59-Add-isMaster-command
Browse files Browse the repository at this point in the history
Implement isMaster with tests for replica and non-replica scenario
  • Loading branch information
tinchodias authored Aug 10, 2019
2 parents 85778a5 + 698d455 commit cb17f07
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 1 deletion.
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/Mongo.class/instance/closeIfOpen.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
operations
closeIfOpen
self isOpen ifTrue: [ self close ]
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/Mongo.class/instance/isMaster.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
operations
isMaster
^ self admin isMaster
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
operations
isMaster

| reply |
reply := self command: (OrderedDictionary new at: #ismaster put: 1; yourself).

^ MongoIsMaster with: reply
12 changes: 12 additions & 0 deletions mc/Mongo-Core.package/MongoIsMaster.class/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
I represent the response of a "isMaster" command, defined by documentation in the following way:

isMaster returns a document that describes the role of the mongod instance. If the optional field saslSupportedMechs is specified, the command also returns an array of SASL mechanisms used to create the specified user’s credentials.

If the instance is a member of a replica set, then isMaster returns a subset of the replica set configuration and status including whether or not the instance is the primary of the replica set.

When sent to a mongod instance that is not a member of a replica set, isMaster returns a subset of this information.

MongoDB drivers and clients use isMaster to determine the state of the replica set members and to discover additional members of a replica set.


Read more at: https://docs.mongodb.com/v4.0/reference/command/isMaster/
5 changes: 5 additions & 0 deletions mc/Mongo-Core.package/MongoIsMaster.class/class/with..st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
instance creation
with: aCollection
^ self basicNew
initializeWith: aCollection;
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
hasPrimary
^ response includesKey: 'primary'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
initialization
initializeWith: aCollection
self initialize.

response := aCollection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
isPrimary
^ (response at: 'ismaster') and: [ self isReplicaSet ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
isReadOnly
^ response at: 'readOnly'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
isReplicaSet
^ response includesKey: 'setName'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
isSecondary
^ response at: 'secondary' ifAbsent: [ false ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
accessing
isWrittable
"A boolean value that reports when this node is writable. If true, then this instance is a primary in a replica set, or a mongos instance, or a standalone mongod.
This field will be false if the instance is a secondary member of a replica set or if the member is an arbiter of a replica set.
Source: https://docs.mongodb.com/manual/reference/command/isMaster/#isMaster.ismaster"

^ response at: 'ismaster'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
primaryUrlString
^ response at: 'primary'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
replicaSetHosts
^ response at: #hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
replicaSetName
^ response at: 'setName'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replica set
replicaSetUrls
^ self replicaSetHosts collect: #asMongoUrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
replica set
urlString
"In a replica set, this is the url of the requested server. Not defined else."

^ response at: 'me'
13 changes: 13 additions & 0 deletions mc/Mongo-Core.package/MongoIsMaster.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commentStamp" : "MartinDias 7/24/2019 12:23",
"super" : "Object",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"response"
],
"name" : "MongoIsMaster",
"type" : "normal"
}
6 changes: 6 additions & 0 deletions mc/Mongo-Core.package/String.extension/instance/asMongoUrl.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*Mongo-Core
asMongoUrl
| url |
url := ZnUrl fromString: self defaultScheme: #mongodb.
url hasPort ifFalse: [ url port: 27017 ].
^ url
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/String.extension/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "String"
}
4 changes: 4 additions & 0 deletions mc/Mongo-Core.package/ZnUrl.extension/instance/asMongoUrl.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*Mongo-Core
asMongoUrl
self assert: (self scheme = #mongodb).
^self
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/ZnUrl.extension/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "ZnUrl"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests
testIsMaster

| response |
response := mongo isMaster.

"Common API is short."
self deny: response isNil.
self deny: response isReadOnly.
self deny: response isReplicaSet.

"Replica Set API that works, anyway."
self deny: response hasPrimary.
self deny: response isPrimary.
self deny: response isSecondary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests
testIsMaster

| response |
response := mongo isMaster.

"Common API is short."
self deny: response isNil.
self deny: response isReadOnly.
self assert: response isReplicaSet.

"Replica Set specific API."
self assert: response hasPrimary.
self assert: response isPrimary.
self deny: response isSecondary.
self assert: response urlString equals: mongo host, ':', mongo port asString.
self assert: response primaryUrlString equals: response urlString.
self assert: response replicaSetHosts equals: #('localhost:27031' 'localhost:27032').
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
running
tearDown
super tearDown.
(mongo isNotNil and: [mongo isOpen]) ifTrue: [mongo close].
mongo ifNotNil: [ mongo closeIfOpen ].

0 comments on commit cb17f07

Please sign in to comment.