|
| 1 | +package editor |
| 2 | + |
| 3 | +import ( |
| 4 | + "cms.csesoc.unsw.edu.au/editor/OT/datamodel" |
| 5 | + "cms.csesoc.unsw.edu.au/editor/OT/operations" |
| 6 | + "cms.csesoc.unsw.edu.au/environment" |
| 7 | + "cms.csesoc.unsw.edu.au/pkg/cmsjson" |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/gorilla/websocket" |
| 10 | +) |
| 11 | + |
| 12 | +// testing_framework is a simple framework that can be used for performing integration tests on the concurrent editor |
| 13 | +// it allows for the observation of client behavior and tracking document server behavior |
| 14 | +type TestingClient struct { |
| 15 | + underlyingClient *clientView |
| 16 | + operationPipe pipe |
| 17 | + terminationPipe func() |
| 18 | +} |
| 19 | + |
| 20 | +func (tC TestingClient) HasTerminated() bool { return len(tC.underlyingClient.sendTerminateSignal) > 0 } |
| 21 | +func (tC TestingClient) WasAcknowledged() bool { |
| 22 | + if len(tC.underlyingClient.sendAcknowledgement) > 0 { |
| 23 | + // potential "unexpected" blocking condition here but for the sake of testing its fine |
| 24 | + // consider two goroutines one of them is this function |
| 25 | + // - this function reads the length and enters this if statement |
| 26 | + // - prior to the next statement another goroutine reads from the sendAck channel emptying it |
| 27 | + // - goroutine running this function is blocked until next ack (very tragic btw) |
| 28 | + // for the purpose of testing tho this should be fine :) |
| 29 | + <-tC.underlyingClient.sendAcknowledgement |
| 30 | + return true |
| 31 | + } |
| 32 | + return false |
| 33 | +} |
| 34 | + |
| 35 | +func (tC TestingClient) GetReceivedOp() operations.Operation { |
| 36 | + if len(tC.underlyingClient.sendOp) == 0 { |
| 37 | + panic("testing client failure: expected a non-zero amount of received operations") |
| 38 | + } |
| 39 | + |
| 40 | + return <-tC.underlyingClient.sendOp |
| 41 | +} |
| 42 | + |
| 43 | +// GetServerState returns the current view that the server sees (as a string) |
| 44 | +func GetServerState(serverId uuid.UUID) string { |
| 45 | + if !environment.IsTestingEnvironment() { |
| 46 | + panic("method can only be called within the context of a test!") |
| 47 | + } |
| 48 | + |
| 49 | + connectedServer := GetDocumentServerFactoryInstance().FetchDocumentServer(serverId) |
| 50 | + connectedServer.stateLock.Lock() |
| 51 | + defer connectedServer.stateLock.Unlock() |
| 52 | + |
| 53 | + return operations.CmsJsonConf.MarshallAST(connectedServer.state) |
| 54 | +} |
| 55 | + |
| 56 | +// CreateServer constructs a server with an initial state, it registers the server under the document manager |
| 57 | +// and returns the server's ID |
| 58 | +func CreateTestingServer(initState string) uuid.UUID { |
| 59 | + if !environment.IsTestingEnvironment() { |
| 60 | + panic("method can only be called within the context of a test!") |
| 61 | + } |
| 62 | + |
| 63 | + var err error |
| 64 | + factory := GetDocumentServerFactoryInstance() |
| 65 | + serverId := uuid.New() |
| 66 | + |
| 67 | + factory.lock.Lock() |
| 68 | + defer factory.lock.Unlock() |
| 69 | + factory.activeServers[serverId] = newDocumentServer() |
| 70 | + |
| 71 | + factory.activeServers[serverId].state, err = cmsjson.UnmarshallAST[datamodel.Document](operations.CmsJsonConf, initState) |
| 72 | + if err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + return serverId |
| 77 | +} |
| 78 | + |
| 79 | +// buildClients starts up n clients and returns their client views as an array |
| 80 | +func BuildTestingClient(serverId uuid.UUID, numClients int) []TestingClient { |
| 81 | + if !environment.IsTestingEnvironment() { |
| 82 | + panic("method can only be called within the context of a test!") |
| 83 | + } |
| 84 | + |
| 85 | + connectedServer := GetDocumentServerFactoryInstance().FetchDocumentServer(serverId) |
| 86 | + |
| 87 | + clients := make([]TestingClient, numClients) |
| 88 | + for clientId := range clients { |
| 89 | + internalView := newClient(&websocket.Conn{}) |
| 90 | + operationPipe, terminationPipe := connectedServer.connectClient(internalView) |
| 91 | + |
| 92 | + clients[clientId] = TestingClient{ |
| 93 | + underlyingClient: internalView, |
| 94 | + operationPipe: operationPipe, |
| 95 | + terminationPipe: terminationPipe, |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return clients |
| 100 | +} |
0 commit comments