Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
3
Indexable
using System.Threading.Tasks;
using NUnit.Framework;
using UniRx;

public class AwaitObservable
{
    [Test]
    public async void AwaitSubject()
    {
        var source = new TaskCompletionSource<int>();

        var subject = new Subject<int>();

        AwaitObservables();
        
        subject.OnNext(5);
        subject.OnCompleted();

        await source.Task;
        
        Assert.IsTrue(source.Task.IsCompleted);
        
        async void AwaitObservables()
        {
            var result = await subject;
            source.SetResult(result);
        }
    }
    
    [Test]
    public async void AwaitSelectedSubject()
    {
        var source = new TaskCompletionSource<Unit>();

        var subject = new Subject<int>();

        AwaitObservables();
        
        subject.OnNext(5);
        subject.OnCompleted();

        await source.Task;
        
        Assert.IsTrue(source.Task.IsCompleted);
        
        async void AwaitObservables()
        {
            var result = await subject.Select(_ => new Unit());
            source.SetResult(result);
        }
    }
    
    [Test]
    public async void AwaitFilteredSubject()
    {
        var source = new TaskCompletionSource<int>();

        var subject = new Subject<int>();

        AwaitObservables();
        
        subject.OnNext(5);
        subject.OnCompleted();

        await source.Task;
        
        Assert.IsTrue(source.Task.IsCompleted);
        
        async void AwaitObservables()
        {
            var result = await subject.Where(_ => true);
            source.SetResult(result);
        }
    }
}