forked from dapr/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weakly typed actor polymorphic and null responses (dapr#1214)
Signed-off-by: Remco Blok <[email protected]> Co-authored-by: Remco Blok <[email protected]> Co-authored-by: Phillip Hoff <[email protected]> Signed-off-by: James Croft <[email protected]>
- Loading branch information
1 parent
2894f85
commit 5dcf216
Showing
7 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/Dapr.E2E.Test.Actors/WeaklyTypedTesting/DerivedResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace Dapr.E2E.Test.Actors.WeaklyTypedTesting | ||
{ | ||
public class DerivedResponse : ResponseBase | ||
{ | ||
public string DerivedProperty { get; set; } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/Dapr.E2E.Test.Actors/WeaklyTypedTesting/IWeaklyTypedTestingActor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using System.Threading.Tasks; | ||
using Dapr.Actors; | ||
|
||
namespace Dapr.E2E.Test.Actors.WeaklyTypedTesting | ||
{ | ||
public interface IWeaklyTypedTestingActor : IPingActor, IActor | ||
{ | ||
Task<ResponseBase> GetPolymorphicResponse(); | ||
|
||
Task<ResponseBase> GetNullResponse(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/Dapr.E2E.Test.Actors/WeaklyTypedTesting/ResponseBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Dapr.E2E.Test.Actors.WeaklyTypedTesting | ||
{ | ||
#if NET7_0_OR_GREATER | ||
[JsonDerivedType(typeof(DerivedResponse), typeDiscriminator: nameof(DerivedResponse))] | ||
#endif | ||
public class ResponseBase | ||
{ | ||
public string BasePropeprty { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using System.Threading.Tasks; | ||
using Dapr.Actors.Runtime; | ||
|
||
namespace Dapr.E2E.Test.Actors.WeaklyTypedTesting | ||
{ | ||
public class WeaklyTypedTestingActor : Actor, IWeaklyTypedTestingActor | ||
{ | ||
public WeaklyTypedTestingActor(ActorHost host) | ||
: base(host) | ||
{ | ||
} | ||
|
||
public Task<ResponseBase> GetNullResponse() | ||
{ | ||
return Task.FromResult<ResponseBase>(null); | ||
} | ||
|
||
public Task<ResponseBase> GetPolymorphicResponse() | ||
{ | ||
var response = new DerivedResponse | ||
{ | ||
BasePropeprty = "Base property value", | ||
DerivedProperty = "Derived property value" | ||
}; | ||
|
||
return Task.FromResult<ResponseBase>(response); | ||
} | ||
|
||
public Task Ping() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
namespace Dapr.E2E.Test | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dapr.Actors; | ||
using Dapr.E2E.Test.Actors.WeaklyTypedTesting; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
public partial class E2ETests : IAsyncLifetime | ||
{ | ||
#if NET8_0_OR_GREATER | ||
[Fact] | ||
public async Task WeaklyTypedActorCanReturnPolymorphicResponse() | ||
{ | ||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); | ||
var pingProxy = this.ProxyFactory.CreateActorProxy<IWeaklyTypedTestingActor>(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
var proxy = this.ProxyFactory.Create(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
|
||
await WaitForActorRuntimeAsync(pingProxy, cts.Token); | ||
|
||
var result = await proxy.InvokeMethodAsync<ResponseBase>(nameof(IWeaklyTypedTestingActor.GetPolymorphicResponse)); | ||
|
||
result.Should().BeOfType<DerivedResponse>().Which.DerivedProperty.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
#else | ||
[Fact] | ||
public async Task WeaklyTypedActorCanReturnDerivedResponse() | ||
{ | ||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); | ||
var pingProxy = this.ProxyFactory.CreateActorProxy<IWeaklyTypedTestingActor>(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
var proxy = this.ProxyFactory.Create(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
|
||
await WaitForActorRuntimeAsync(pingProxy, cts.Token); | ||
|
||
var result = await proxy.InvokeMethodAsync<DerivedResponse>(nameof(IWeaklyTypedTestingActor.GetPolymorphicResponse)); | ||
|
||
result.Should().BeOfType<DerivedResponse>().Which.DerivedProperty.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
#endif | ||
[Fact] | ||
public async Task WeaklyTypedActorCanReturnNullResponse() | ||
{ | ||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); | ||
var pingProxy = this.ProxyFactory.CreateActorProxy<IWeaklyTypedTestingActor>(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
var proxy = this.ProxyFactory.Create(ActorId.CreateRandom(), "WeaklyTypedTestingActor"); | ||
|
||
await WaitForActorRuntimeAsync(pingProxy, cts.Token); | ||
|
||
var result = await proxy.InvokeMethodAsync<ResponseBase>(nameof(IWeaklyTypedTestingActor.GetNullResponse)); | ||
|
||
result.Should().BeNull(); | ||
} | ||
} | ||
} |