소스 검색

`waitFor:`, refactoring

Herby Vojčík 7 년 전
부모
커밋
37e3625355
3개의 변경된 파일49개의 추가작업 그리고 6개의 파일을 삭제
  1. 6 0
      src/Znock/ZnockBase.class.st
  2. 21 6
      src/Znock/ZnockClient.class.st
  3. 22 0
      src/Znock/ZnockExpectation.class.st

+ 6 - 0
src/Znock/ZnockBase.class.st

@@ -124,3 +124,9 @@ ZnockBase >> url: anObject [
 	builtClient url: anObject.
 	builtClient request url scheme: scheme
 ]
+
+{ #category : #public }
+ZnockBase >> waitFor: aDuration [
+	self response.
+	builtClient delay: aDuration
+]

+ 21 - 6
src/Znock/ZnockClient.class.st

@@ -16,12 +16,27 @@ ZnockClient class >> newWithInterceptor: aZnock [
 
 { #category : #'private protocol' }
 ZnockClient >> executeRequestResponse [
-	^ (interceptor consumeExpectationFor: self)
-		ifNotNil: [ :expectation |
-			response := expectation response.
-			(response respondsTo: #signal) ifTrue: [ response signal ].
-			response contents ]
-		ifNil: [ super executeRequestResponse ]
+	| expectation |
+	expectation := interceptor consumeExpectationFor: self.
+	expectation ifNil: [ ^ super executeRequestResponse ].
+	expectation delay ifNotNil: [ :delay | self fakeDelay: delay ].
+	self fakeResponse: expectation response.
+	^ response contents
+]
+
+{ #category : #'private protocol' }
+ZnockClient >> fakeDelay: aDuration [
+	| localTimeout |
+	localTimeout := self timeout seconds.
+	localTimeout <= aDuration
+		ifTrue: [ localTimeout asDelay wait. ConnectionTimedOut signal: 'Znock timeout' ]
+		ifFalse: [ aDuration asDelay wait ]
+]
+
+{ #category : #'private protocol' }
+ZnockClient >> fakeResponse: aZnResponse [
+	response := aZnResponse.
+	(response respondsTo: #signal) ifTrue: [ response signal ]
 ]
 
 { #category : #accessing }

+ 22 - 0
src/Znock/ZnockExpectation.class.st

@@ -1,14 +1,36 @@
 Class {
 	#name : #ZnockExpectation,
 	#superclass : #ZnClient,
+	#instVars : [
+		'delay'
+	],
 	#category : #Znock
 }
 
+{ #category : #accessing }
+ZnockExpectation >> delay [
+	^ delay
+]
+
+{ #category : #accessing }
+ZnockExpectation >> delay: anObject [
+	delay := anObject
+]
+
 { #category : #operations }
 ZnockExpectation >> execute [
 	"Do nothing"
 ]
 
+{ #category : #initialization }
+ZnockExpectation >> initialize [
+
+	super initialize.
+
+	delay := nil.
+	finalizer := nil.
+]
+
 { #category : #accessing }
 ZnockExpectation >> response: aZnResponse [
 	response := aZnResponse