ModelTests.vb 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Shared.TestHooks
Imports Moq

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.IntelliSense

    <Trait(Traits.Feature, Traits.Features.DebuggingIntelliSense)>
    Public Class ModelTests
        Public Sub New()
            TestWorkspace.ResetThreadAffinity()
        End Sub

B
beep boop 已提交
18
        Public Class Model
19 20 21 22 23
        End Class

        Private Class TestModelComputation
            Inherits ModelComputation(Of Model)

B
beep boop 已提交
24
            Public Sub New(controller As IController(Of Model))
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
                MyBase.New(controller, TaskScheduler.Default)
            End Sub

            Friend Shared Function Create(Optional controller As IController(Of Model) = Nothing) As TestModelComputation
                If controller Is Nothing Then
                    Dim mock = New Mock(Of IController(Of Model))
                    controller = mock.Object
                End If

                Return New TestModelComputation(controller)
            End Function

            Friend Sub Wait()
                WaitForController()
            End Sub
        End Class

J
Jared Parsons 已提交
42
        <WpfFact(Skip:="xunit wait")>
43 44 45 46 47 48 49 50 51
        Public Sub ChainingTaskStartsAsyncOperation()
            Dim controller = New Mock(Of IController(Of Model))
            Dim modelComputation = TestModelComputation.Create(controller:=controller.Object)

            modelComputation.ChainTaskAndNotifyControllerWhenFinished(Function(m) m)

            controller.Verify(Sub(c) c.BeginAsyncOperation())
        End Sub

52
        <WpfFact>
53 54 55 56 57 58 59 60 61 62 63
        Public Sub ChainingTaskThatCompletesNotifiesController()
            Dim controller = New Mock(Of IController(Of Model))
            Dim modelComputation = TestModelComputation.Create(controller:=controller.Object)
            Dim model = New Model()

            modelComputation.ChainTaskAndNotifyControllerWhenFinished(Function(m) model)
            modelComputation.Wait()

            controller.Verify(Sub(c) c.OnModelUpdated(model))
        End Sub

64
        <WpfFact>
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        Public Sub ControllerIsOnlyUpdatedAfterLastTaskCompletes()
            Dim controller = New Mock(Of IController(Of Model))
            Dim modelComputation = TestModelComputation.Create(controller:=controller.Object)
            Dim model = New Model()
            Dim gate = New Object

            Monitor.Enter(gate)
            modelComputation.ChainTaskAndNotifyControllerWhenFinished(Function(m)
                                                                          SyncLock gate
                                                                              Return Nothing
                                                                          End SyncLock
                                                                      End Function)
            modelComputation.ChainTaskAndNotifyControllerWhenFinished(Function(m) model)
            Monitor.Exit(gate)
            modelComputation.Wait()

            controller.Verify(Sub(c) c.OnModelUpdated(model), Times.Once)
        End Sub

J
Jared Parsons 已提交
84
        <WpfFact(Skip:="true")>
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        Public Sub ControllerIsNotUpdatedIfComputationIsCancelled()
            Dim controller = New Mock(Of IController(Of Model))
            Dim token = New Mock(Of IAsyncToken)
            controller.Setup(Function(c) c.BeginAsyncOperation()).Returns(token.Object)
            Dim modelComputation = TestModelComputation.Create(controller:=controller.Object)
            Dim model = New Model()
            Dim checkpoint1 = New Checkpoint
            Dim checkpoint2 = New Checkpoint
            Dim checkpoint3 = New Checkpoint

            token.Setup(Sub(t) t.Dispose()).Callback(Sub() checkpoint3.Release())

            modelComputation.ChainTaskAndNotifyControllerWhenFinished(Function(m, c)
                                                                          checkpoint1.Release()
                                                                          checkpoint2.Task.Wait()
                                                                          c.ThrowIfCancellationRequested()
                                                                          Return Task.FromResult(model)
                                                                      End Function)
            checkpoint1.Task.Wait()
            modelComputation.Stop()
            checkpoint2.Release()
            checkpoint3.PumpingWait()

            controller.Verify(Sub(c) c.OnModelUpdated(model), Times.Never)
        End Sub

    End Class
End Namespace