Skip to content

Commit

Permalink
Cleaning tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guillep committed Sep 7, 2023
1 parent 4fff7b9 commit 4b1754a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 72 deletions.
55 changes: 55 additions & 0 deletions src/MethodProxies-Tests/MpAbstractMethodProxyTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Class {
#name : #MpAbstractMethodProxyTest,
#superclass : #TestCase,
#instVars : [
'trackedWrappers'
],
#category : #'MethodProxies-Tests'
}

{ #category : #testing }
MpAbstractMethodProxyTest class >> isAbstract [

^ self == MpAbstractMethodProxyTest
]

{ #category : #'tests - dead representation' }
MpAbstractMethodProxyTest >> installMethodProxy: aMethodProxy [

trackedWrappers add: aMethodProxy.
aMethodProxy install.

]

{ #category : #initialization }
MpAbstractMethodProxyTest >> setUp [

super setUp.
trackedWrappers := OrderedCollection new
]

{ #category : #initialization }
MpAbstractMethodProxyTest >> tearDown [

| stillInstalled |

"Uninstall proxies using a fixed point approach.
This is to cover a problem of proxies wrapping proxies for now"
[
stillInstalled := trackedWrappers select: [ :e | e isInstalled ].
stillInstalled isEmpty ] whileFalse: [
stillInstalled do: [ :each |
[
each uninstall.
trackedWrappers remove: each ]
on: Error
do: [ :e | "continue" ] ] ].

"Give me the guarantee that we did not leave proxies installed in the system"
(MpMethodProxy allInstances anySatisfy: [ :e | e isInstalled ])
ifTrue: [
self error:
'Proxies still installed after test: ' , testSelector asString ].

super tearDown
]
46 changes: 1 addition & 45 deletions src/MethodProxies-Tests/MpMethodProxyTest.class.st
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
Class {
#name : #MpMethodProxyTest,
#superclass : #TestCase,
#instVars : [
'trackedWrappers'
],
#superclass : #MpAbstractMethodProxyTest,
#category : #'MethodProxies-Tests'
}

Expand Down Expand Up @@ -32,47 +29,6 @@ MpMethodProxyTest >> handlerClass [
^ MpHandler
]

{ #category : #'tests - dead representation' }
MpMethodProxyTest >> installMethodProxy: aMethodProxy [

trackedWrappers add: aMethodProxy.
aMethodProxy install.

]

{ #category : #initialization }
MpMethodProxyTest >> setUp [

super setUp.
trackedWrappers := OrderedCollection new
]

{ #category : #initialization }
MpMethodProxyTest >> tearDown [

| stillInstalled |

"Uninstall proxies using a fixed point approach.
This is to cover a problem of proxies wrapping proxies for now"
[
stillInstalled := trackedWrappers select: [ :e | e isInstalled ].
stillInstalled isEmpty ] whileFalse: [
stillInstalled do: [ :each |
[
each uninstall.
trackedWrappers remove: each ]
on: Error
do: [ :e | "continue" ] ] ].

"Give me the guarantee that we did not leave proxies installed in the system"
(MpMethodProxy allInstances anySatisfy: [ :e | e isInstalled ])
ifTrue: [
self error:
'Proxies still installed after test: ' , testSelector asString ].

super tearDown
]

{ #category : #'tests - safety' }
MpMethodProxyTest >> testCanRunConcurrently [
"This tests the ability of method proxies to not influence each other between threads."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Class {
#name : #MpAllocationProfilerHandlerTest,
#superclass : #MpMethodProxyTest,
#superclass : #MpAbstractMethodProxyTest,
#category : #'MethodProxiesExamples-Tests'
}

Expand Down

This file was deleted.

58 changes: 48 additions & 10 deletions src/MethodProxiesExamples-Tests/MpCountingMethodProxyTest.class.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Class {
#name : #MpCountingMethodProxyTest,
#superclass : #MpMethodProxyTest,
#superclass : #MpAbstractMethodProxyTest,
#category : #'MethodProxiesExamples-Tests'
}

Expand All @@ -11,18 +11,56 @@ MpCountingMethodProxyTest >> handlerClass [
]

{ #category : #tests }
MpCountingMethodProxyTest >> testCounts [
MpCountingMethodProxyTest >> testInstalledButNotCalledCounts0 [

| proxy instance handler |
[ proxy := MpMethodProxy
on: #methodOne
inClass: MpClassA
handler: (handler := self handlerClass new).
proxy install.
proxy := MpMethodProxy
onMethod: MpClassA >> #methodOne
handler: (handler := self handlerClass new).
self installMethodProxy: proxy.

instance := MpClassA new.
self assert: handler count equals: 0
]

{ #category : #tests }
MpCountingMethodProxyTest >> testInstalledCalledOnceCounts1 [

| proxy instance handler |
proxy := MpMethodProxy
onMethod: MpClassA >> #methodOne
handler: (handler := self handlerClass new).
self installMethodProxy: proxy.

instance := MpClassA new.
self assert: handler count equals: 0.
instance methodOne.
self assert: handler count equals: 1.
self assert: handler count equals: 1
]

{ #category : #tests }
MpCountingMethodProxyTest >> testInstalledCalledTwiceCounts2 [

| proxy instance handler instance2 |
proxy := MpMethodProxy
onMethod: MpClassA >> #methodOne
handler: (handler := self handlerClass new).
self installMethodProxy: proxy.

instance := MpClassA new.
instance methodOne.
self assert: handler count equals: 2 ] ensure: [ proxy uninstall ]

instance2 := MpClassA new.
instance2 methodOne.

self assert: handler count equals: 2
]

{ #category : #tests }
MpCountingMethodProxyTest >> testNonInstalledCounts0 [

| proxy handler |
proxy := MpMethodProxy
onMethod: MpClassA >> #methodOne
handler: (handler := self handlerClass new).
self assert: handler count equals: 0
]
14 changes: 7 additions & 7 deletions src/MethodProxiesExamples/MpAllocationProfilerHandler.class.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"
I'm a simple little profiler that can store all the objects returned by the spyied method.
I'm an example handler for profiling purposes that stores all the objects returned by the spyied method
```
h := MpAllocationProfilerHandler new.
p1 := MpMethodProxy
Expand Down Expand Up @@ -45,14 +46,13 @@ MpAllocationProfilerHandler >> allocations [
MpAllocationProfilerHandler >> captureCallingContext [

| runWithInContext |

"Find the context of #run:with:in:"
"Find the context of #run:with:in:"
runWithInContext := thisContext sender.
[ runWithInContext isNil
or: [ runWithInContext method isCompiledMethod and: [runWithInContext method selector = #run:with:in:] ] ]
[
runWithInContext isNil or: [ runWithInContext method primitive = 198 ] ]
whileFalse: [ runWithInContext := runWithInContext sender ].
"Find the real sender"

"Find the real sender"
^ runWithInContext ifNotNil: [ runWithInContext sender ]
]

Expand Down
2 changes: 1 addition & 1 deletion src/MethodProxiesExamples/MpCalledHandler.class.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"
I'm a little example that reports if the spyied method has been executed.
I'm an example handler that reports if the spyied method has been executed
"
Class {
#name : #MpCalledHandler,
Expand Down
2 changes: 1 addition & 1 deletion src/MethodProxiesExamples/MpCountingHandler.class.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"
I'm counting the number of times I'm executed.
I'm an example handler that counts all the times a method has been invoked
"
Class {
#name : #MpCountingHandler,
Expand Down
3 changes: 2 additions & 1 deletion src/MethodProxiesExamples/MpFailingHandlerMock.class.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"
I'm a mock for tests that checks that the infrastructure does not collapse when a handler is failing.
I'm an example handler for testing purposes that fails on before.
I'm used to check that the infrastructure does not collapse when a handler fails
"
Class {
#name : #MpFailingHandlerMock,
Expand Down
3 changes: 2 additions & 1 deletion src/MethodProxiesExamples/MpProfilingHandler.class.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"
I'm a more advanced proxy that propagates itself during execution.
When a proxy is executed, before letting the execution runs, it installs itself on all the implementators of the methods used in the method.
When a proxy is executed, before letting the execution runs, it installs itself on all the implementors of the messages sent in the method.
```
proxy := nil.
proxies := nil.
Expand Down

0 comments on commit 4b1754a

Please sign in to comment.