diff --git a/src/MethodProxies-Tests/MpAbstractMethodProxyTest.class.st b/src/MethodProxies-Tests/MpAbstractMethodProxyTest.class.st new file mode 100644 index 0000000..10b0769 --- /dev/null +++ b/src/MethodProxies-Tests/MpAbstractMethodProxyTest.class.st @@ -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 +] diff --git a/src/MethodProxies-Tests/MpMethodProxyTest.class.st b/src/MethodProxies-Tests/MpMethodProxyTest.class.st index 2c14d80..71c4e05 100644 --- a/src/MethodProxies-Tests/MpMethodProxyTest.class.st +++ b/src/MethodProxies-Tests/MpMethodProxyTest.class.st @@ -1,9 +1,6 @@ Class { #name : #MpMethodProxyTest, - #superclass : #TestCase, - #instVars : [ - 'trackedWrappers' - ], + #superclass : #MpAbstractMethodProxyTest, #category : #'MethodProxies-Tests' } @@ -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." diff --git a/src/MethodProxiesExamples-Tests/MpAllocationProfilerHandlerTest.class.st b/src/MethodProxiesExamples-Tests/MpAllocationProfilerHandlerTest.class.st index 8741f13..9f3736d 100644 --- a/src/MethodProxiesExamples-Tests/MpAllocationProfilerHandlerTest.class.st +++ b/src/MethodProxiesExamples-Tests/MpAllocationProfilerHandlerTest.class.st @@ -1,6 +1,6 @@ Class { #name : #MpAllocationProfilerHandlerTest, - #superclass : #MpMethodProxyTest, + #superclass : #MpAbstractMethodProxyTest, #category : #'MethodProxiesExamples-Tests' } diff --git a/src/MethodProxiesExamples-Tests/MpCalledMethodProxyTest.class.st b/src/MethodProxiesExamples-Tests/MpCalledMethodProxyTest.class.st deleted file mode 100644 index 9e83b27..0000000 --- a/src/MethodProxiesExamples-Tests/MpCalledMethodProxyTest.class.st +++ /dev/null @@ -1,5 +0,0 @@ -Class { - #name : #MpCalledMethodProxyTest, - #superclass : #MpMethodProxyTest, - #category : #'MethodProxiesExamples-Tests' -} diff --git a/src/MethodProxiesExamples-Tests/MpCountingMethodProxyTest.class.st b/src/MethodProxiesExamples-Tests/MpCountingMethodProxyTest.class.st index a7b769e..8689824 100644 --- a/src/MethodProxiesExamples-Tests/MpCountingMethodProxyTest.class.st +++ b/src/MethodProxiesExamples-Tests/MpCountingMethodProxyTest.class.st @@ -1,6 +1,6 @@ Class { #name : #MpCountingMethodProxyTest, - #superclass : #MpMethodProxyTest, + #superclass : #MpAbstractMethodProxyTest, #category : #'MethodProxiesExamples-Tests' } @@ -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 ] diff --git a/src/MethodProxiesExamples/MpAllocationProfilerHandler.class.st b/src/MethodProxiesExamples/MpAllocationProfilerHandler.class.st index 8e805ca..afe5318 100644 --- a/src/MethodProxiesExamples/MpAllocationProfilerHandler.class.st +++ b/src/MethodProxiesExamples/MpAllocationProfilerHandler.class.st @@ -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 @@ -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 ] ] diff --git a/src/MethodProxiesExamples/MpCalledHandler.class.st b/src/MethodProxiesExamples/MpCalledHandler.class.st index 51f24ff..2f1ebc5 100644 --- a/src/MethodProxiesExamples/MpCalledHandler.class.st +++ b/src/MethodProxiesExamples/MpCalledHandler.class.st @@ -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, diff --git a/src/MethodProxiesExamples/MpCountingHandler.class.st b/src/MethodProxiesExamples/MpCountingHandler.class.st index 8bfc61b..a4d75fa 100644 --- a/src/MethodProxiesExamples/MpCountingHandler.class.st +++ b/src/MethodProxiesExamples/MpCountingHandler.class.st @@ -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, diff --git a/src/MethodProxiesExamples/MpFailingHandlerMock.class.st b/src/MethodProxiesExamples/MpFailingHandlerMock.class.st index 74b8f80..05c5d1b 100644 --- a/src/MethodProxiesExamples/MpFailingHandlerMock.class.st +++ b/src/MethodProxiesExamples/MpFailingHandlerMock.class.st @@ -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, diff --git a/src/MethodProxiesExamples/MpProfilingHandler.class.st b/src/MethodProxiesExamples/MpProfilingHandler.class.st index d554e1b..ec2e738 100644 --- a/src/MethodProxiesExamples/MpProfilingHandler.class.st +++ b/src/MethodProxiesExamples/MpProfilingHandler.class.st @@ -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.